The signature for add_sorted has been updated to the following:
llist *add_sorted_country(gymnast_info *ginfo, llist *head); llist *add_sorted_total(gymnast_info *ginfo, llist *head);This allows you to return a pointer to the new beginning (head) of the list when
llist *add_sorted_country(gymnast_info *ginfo, llist *head); llist *add_sorted_total(gymnast_info *ginfo, llist *head);you insert something to the front.
The signature for create_gymnast has been updated to the following:
gymnast_info *create_gymnast(char *first, char *last, enum Country c, float *scores);because Country is an enum. It needs the enum specifier each time you use the type name.
You are expected to complete this assignment individually. If you need help, you are invited to come to office hours and/or ask questions on piazza. Clarification questions about the assignments may be asked publicly. Once you have specific bugs related to your code, make the posts private.
This homework has several problems. We are also providing you with some resources on printf and error handling for this assignment.
You should submit four files for this assignment ( hw4.h, hw4.c, hw4_main.c, and Makefile) in your subversion repository as directed below.
You already created hw4.c and hw4.h in the warmup. Continue adding to those files for this homework.
You need to add the skeleton code so that your program will minimally execute. You must do this in case you do not complete your assignment. Our testing infrastructure needs to compile and execute even if you did not complete the entire assignment. Refer to past assignments on how to make skeleton code.
Write a function that allocates memory and fills in the fields for a gymnast. Make sure that you duplicate the memory for the strings.
gymnast_info *create_gymnast(char *first, char *last, enum Country c, float *scores);
If any of the inputs are NULL, then return NULL.
Note: You may use string functions for this problem. Don't forget to complete this portion, test it, and commit it before moving on!You now have enough knowledge to create a better test function. Instead of having a single function for each test, you can make a function for each test suite, or set of tests testing the same function. The barrier was that you would need two pieces of information - how many tests and how many tests were correct. We can use this by using out parameters.
An out parameter is a variable in the caller's space whose address is sent in to a function. The function then fills in the appropriate value inside the function. For example, in the following code, sum is an out parameter:
void sum(int *array, int length, int *sum) { *sum = 0; int i; for(i=0;i<length;i++) *sum += array[i]; }This would be called in the following way:
int array[] = {5,3,8,0,4}; int s; sum(array,5,&s);A few comments. Note that s is a variable in the caller's space, and its address was sent in. If we did the following, execution would result in a segmentation fault:
int array[] = {5,3,8,0,4}; int *s = NULL; sum(array,5,s);
In this case, s is a pointer, but it has not been assigned to point to a valid variable. Therefore, it cannot be used as the out parameter.
In addition, this is typically only done when the amount of information returned is more than a single variable. In the case above, it would have been better to return sum rather than communicate the answer through an out parameter. It was chosen as a simple case, not an appropriate case.
You are to write a single test function, test_max_country_event_suite, that contains all of the code to test max_country and max_event, including creating the linked list and populating it with gymnasts. The signature is the following:
void test_max_country_event_suite(int *num_tests, int *num_passed);
There are various sorting algorithms that work with arrays. With linked lists, they are much more complex. Instead of sorting an existing array, the best way is to insert items in sorted order. That is, we make sure that at all times, the array is sorted, allowing us to easily maintain sorted order by carefully inserting new items.
Note: I am not asking you to sort an existing, unsorted linked list. We will also not test your function by calling it with an unsorted list.
You are going to write two different functions that insert gymnasts in sorted order in two different ways - one by country and one by total score.
llist *add_sorted_country(gymnast_info *ginfo, llist *head); llist *add_sorted_total(gymnast_info *ginfo, llist *head);
Now you are going to create your event loop for this application. This will look very similar to last homework's, but with different questions and actions.
void event_loop() { printf("What would you like to do?\n"); printf("(0 = quit, 1 = create gymnast, \n"); printf("2 = print max country, 3 = print max event,\n"); printf("4 = print by country, 5 = print by overall): "); // read in answer, interpret it // if it is create gymnast: printf("What is the first name? "); printf("What is the last name? "); printf("What country? "); printf("What score for uneven bars? "); printf("What score for vault? "); printf("What score for beam? "); printf("What score for floor? "); // create the gymnast. // if it is print max country, then print out the answer printf("The winning country is %s\n", ); // if print max event printf("Which event?\n"); printf("(0 = bars, 1 = vault, 2 = beam, 3 = floor): "); printf("The winning gymnast is %s %s\n"); printf("with a score of %f\n"); // print by country // use the provided print list function // print by overall // use the provided print list function }
Your job is to fill in the code that reads in each answer, translates it so it is useful to the program, and does any action based on it. You also need to loop rather than just go through once.
Testing hint: Keep track of all of the operations you do to create a list of gymnasts and test your code. Place those in a file, say testinput.txt, in order. Put in exactly everything that you type in the keyboard as you are running your session. Then, to make the same thing happen again without retyping it, do the following:
$ ./a.out < testinput.txtThis will send all those keystrokes as if you had typed them. This way you can have much better testing since you don't have to laboriously type it in each time!
$ svn add hw4.h hw4.c test_hw4.c
$ svn commit -m "hw4 complete"