In Class Week13, Tues

  1. In your week12 subdirectory, copy over the babies.c file:
         cp /home/newhall/public/cs21/week12/babies.c .
         

  2. Open up babies.c. This is starting point of a program to which you will add code to read in baby information from a file into an array of babyType.

      This part has been done for you:

    1. open the file named "baby.data"
    2. read in the first line of the file which contains the number of babies.
           # to read in a line of up to 50 chars long from a file into a string 
           # (str is an allocated string, and infile is a FILE* to file to read from):
           fgets(str, 50, infile); 
      
           # fgets put the '\n' in the string it returns before adding '\0' 
           # if it reads in a '\n', you should replace '\n' in the string with '\0'
           str[strlen(str)-1] = '\0';
      
           # you can then use the function atoi to convert a string to an int
           num = atoi(str);     # if str is "1234", this returns the int value 1234
           

      You need to add:

    3. Dynamically allocate space for an array of that many babyType
    4. Create a function InitABabay that takes a FILE * for the file to read from, and return a babyType initialized to name, length, weight_lbs, and weight_oz values read in from the file

      You probably will want to make use of the functions atoi() and atof().

    5. Iterate through the the allocated array and read in baby information from the file by making calls to the previous function.
    6. Print out the values in your array.
    7. Don't forget to free() any space you malloc(). You should run your program using valgrind to check.

  3. If you have time, write functions that take an array of babyType and its length as parameters and calculates

    1. The biggest baby
    2. The smallest baby
    3. The longest baby
    4. The shortest baby