// speed_friending.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // Finds the first person in a room who shares an individual's // favourite colour. #include #include #include // Constants #define MAX_PEOPLE 100 #define MAX_LENGTH 32 struct person { char name[MAX_LENGTH]; char favourite_colour[MAX_LENGTH]; }; // Function prototypes void find_match(struct person individual, struct person people[MAX_PEOPLE], int num_people); struct person read_person(void); int read_people(struct person people[MAX_PEOPLE]); int scan_token(char *buffer, int buffer_size); // ----------------------------------------------------------------------------- //////////////// DO NOT CHANGE THE MAIN FUNCTION /////////////////////////////// int main(void) { printf("Enter your name and favourite colour: "); struct person individual = read_person(); struct person people[MAX_PEOPLE]; printf("Enter everyone else in the room:\n"); int num_people = read_people(people); printf("\n"); find_match(individual, people, num_people); return 0; } // ----------------------------------------------------------------------------- // YOUR FUNCTIONS // ----------------------------------------------------------------------------- // Find the first person in the array who shares the individual's // favourite colour, and print the result. void find_match(struct person individual, struct person people[MAX_PEOPLE], int num_people) { // TODO: Complete this function } // ----------------------------------------------------------------------------- // PROVIDED FUNCTIONS // ----------------------------------------------------------------------------- // DO NOT CHANGE ANY OF THE CODE BELOW HERE // Read people from stdin into the given array, returning how many int read_people(struct person people[MAX_PEOPLE]) { int num_people = 0; while (num_people < MAX_PEOPLE && scan_token(people[num_people].name, MAX_LENGTH) == 1 && scan_token(people[num_people].favourite_colour, MAX_LENGTH) == 1) { num_people++; } return num_people; } // Read a single person from stdin struct person read_person(void) { struct person p; scan_token(p.name, MAX_LENGTH); scan_token(p.favourite_colour, MAX_LENGTH); return p; } // scan a single whitespace-separated token from stdin int scan_token(char *buffer, int buffer_size) { if (buffer_size == 0) { return 0; } char c; int i = 0; int num_scanned = 0; scanf(" "); while (i < buffer_size - 1 && (num_scanned = scanf("%c", &c)) == 1 && !isspace(c)) { buffer[i++] = c; } if (i > 0) { buffer[i] = '\0'; } return num_scanned; }