Thread: Problems with runtime error

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Problems with runtime error

    I am stumped with this exercise. I'm supposed to write a program that "simulates dating".
    The original circle is described by a string circle containing lowercase letters for women
    and uppercase letter for men. The last one in circle is actually adjacent to the rst one.
    Starting with the rst person in circle, we count from 1 to k around the circle, wrapping
    around if necessary, and letting the k-th person be the rst chooser. Letters nearer the
    beginning of the alphabet represent more desirable individuals, and the chooser always
    chooses the most desirable remaining member of the opposite sex. Starting with the
    next remaining person after the last chooser we again count from one to k (counting only
    people still remaining in circle) to determine the next chooser. This continues until the
    party breaks up.

    Right now, I'm getting a big runtime crash, don't know why.
    Then, I can't get them to prefer the letters closer the beginning of the alphabet.

    Any help would greatly appreciated.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    #define MAX_PERSONS 50
    #define MAX_CH 100
    
    
    void dating(char *circle, char *dates, int k);
    int my_strlen(char *s);
    
    int main(int argc, char *argv[]) 
    {
    	char date_array[50];
    	char *circle;
    	char *dates = date_array;
    
    
    	circle = argv[1];
    	int k = atoi(argv[2]);
    
    	dating(circle, dates, k);
    
    	printf("%s\n", date_array);
    
    
    	return 0;
    }
    
    void dating(char* circle, char* dates, int k)
    {
    	int circleLen = my_strlen(circle);
    	int end = 0;
    	//int empty = 0;
    	int gender;
    
    	char male[26] = "ZYXWVUTSRPQONMLKJIHGFEDCBA";
    	char female[26] = "zyxwvutsrqponmlkjihgfedcba";
    
    	while((my_strlen(circle)) > 0)
    	{
    		int temp = k;
    
    		while(end == 0)
    		{
    			if(temp > circleLen)
    			{
    				temp -= circleLen;
    			}
    			else
    			{
    				end = 1;
    			}
    		}
    		
    		if(isupper(circle[temp]))
    			gender = 1;
    		else
    			gender = 0;
    
    		if(gender == 1)
    		{
    			while(temp < (my_strlen(circle)))
    			{
    				int rank = 0;
    				int place;
    
    				for(int a = 0; a < 26; a++)
    				{
    
    					if((circle[temp] == female[a])&&(a > rank))
    					{
    						rank = a;
    						place = temp;
    					}
    				}
    
    				if(rank > 0) //writing a date to *dates
    				{
    					dates += sprintf(dates, "%c%c ", circle[place], male[rank]);
    					
    					char *newCircle;
    
    					for( int d = 0; d < place; d++)
    					{
    						newCircle += sprintf(dates, "%c%c ", circle[d]);
    					}
    
    					for( int d = (place +1); d < (my_strlen(circle)); d++)
    					{
    						newCircle += sprintf(dates, "%c%c ", circle[d]);
    					}
    
    					circle = newCircle;
    				}
    
    				printf("%c\n", circle[temp]);
    
    				temp++;
    			}
    		}
    		else
    		{
    			while(temp < (my_strlen(circle)))
    			{
    				int rank = 0;
    				int place;
    
    				for(int a = 0; a < 26; a++)
    				{
    					if((circle[temp] == male[a])&&(a > rank))
    					{
    						rank = a;
    						place = temp;
    					}
    				}
    
    				if(rank > 0) //writing a date to *dates
    				{
    					dates += sprintf(dates, "%c%c ", circle[place], female[rank]);
    					
    					char *newCircle;
    
    					for( int d = 0; d < place; d++)
    					{
    						newCircle += sprintf(dates, "%c%c ", circle[d]);
    
    					}
    
    					for( int d = (place +1); d < (my_strlen(circle)); d++)
    					{
    						newCircle += sprintf(dates, "%c%c ", circle[d]);
    					}
    
    					circle = newCircle;
    				}
    
    				temp++;
    			}
    		}
    
    	}
    
    }
    
    int my_strlen(char *s)
    {
       int i = 0;
    
       // Count until you reach the end-of-string termination character
       while (*s != '\0')
       {
          i++;
          s++;
       }
       return i;
    }
    Last edited by azrael; 03-20-2009 at 03:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM