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;
}