Goals for this Pre-lab

  • Practice strings, structs, more linked lists
  • Get your files ready for the HW

Remember to create a new directory in your repository for this lab. (Refer to previous labs for how if you have forgotten)

Review your notes about strings, structs, and linked lists. Then think about the questions in lab5questions.html. You are not turning these in because there is no lab period!

Setup

During this warmup, you are going to implement several functions that exercise strings and pointers. If you are using Duet Programming, do not forget to trade off after each function. The functions are ordered to provide specific practice to each student. Only the first problem is necessary for the HW, so if you feel confident that you know the material, you may complete the first and move directly on to the homework.

You will be adding to these functions in the homework, so do not create warmup files - instead, place all of your code in hw5.h and hw5.c.

All of the linked list operations will hold gymnasts rather than integers. The data declarations necessary for this assignment are below. These go in hw5.h, NOT hw5.c.

enum Country { USA=0, MEXICO, BRAZIL, RUSSIA, CHINA, SAFRICA, 
			CHILE, ROMANIA };

enum Event { UNEVEN=0, VAULT, BEAM, FLOOR };

#define NUM_EVENTS 4

typedef struct {
 char *lastname;
 char *firstname;
 enum Country country;
 float scores[NUM_EVENTS];
} gymnast_info;

// for the linked list, we will use the following struct
typedef struct _llist llist;

struct _llist{
 gymnast_info *g;
 llist *next;
};

// extern defines a global variable that all files can use. It is in 
// the .h file, then it needs to be declared again inside a single .c file
// without the extern keyword preceeding it.
extern char *country_strings[];
void print_gymnast(gymnast_info *g, FILE *fp);
void print_list(llist *head, FILE *fp);
The code below goes inside your hw5.c file. This includes the declaration of the global variable country_strings as well as a print function I am providing so that all students' print functions match.
char *country_strings[] = { "USA", "Mexico", "Brazil", "Russia", "China", "South Africa", "Chile", "Romania"}; void print_gymnast(gymnast_info *g, FILE *fp) { fprintf(fp,"%s %s ",g->firstname, g->lastname); fprintf(fp,"%s:",country_strings[g->country]); int i; for(i=0;i<NUM_EVENTS;i++) fprintf(fp,"%f, ",g->scores[i]); fprintf(fp,"\n"); } void print_list(llist *head, FILE *fp) { llist *tmp; for(tmp = head; tmp != NULL; tmp = tmp->next) print_gymnast(tmp->g,fp); }
To call print_gymnast or print_list to print to the screen, use stdout as the second argument. It also allows you to print to a file (which we may use in testing).

Exercises

Problem 1: create_gymnast

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.

Problem 2: insert_head

Write a function that adds a gymnast to the "head" or beginning of a list. The new first node of the list will point to ginfo. It returns a pointer to the first node in the modified list.

llist* isnert_head(gymnast_info *ginfo, llist *list);
Problem 3: insert_country(gymnast_info *ginfo, llist *list)

Write a function that inserts a gymnast immediately after the first gymnast from the same country. If there are no gymnasts in the list from this country, the new gymnast is inserted at the end of the list. It returns a pointer to the beginning of the modified list.

llist* insert_country(gymnast_info *ginfo, llist *list);
Problem 4: max_event

Write a function that returns the gymnast with the highest score in a single event. It returns a pointer to the gymnast_info, not the node. If the event number is too high, return NULL.

gymnast_info* max_event(llist *list, unsigned int event);