Convert grades task to structs

The task is based on the same data as was the task from last week! The solution provided uses arrays to store all of the data from the data file. Your task is to modify it to make it use structures and add a bit of functionality to it.

Download the starter code: 04_to_struct.zip

Confused about the header file? We talked about this during programming 1: https://blue.pri.ee/ttu/programming-i/labs/13-black-box/#Using_custom_header_files

Data file structure: <subject> <grade count> <grades>

General task description:

  • Code must be correctly divided between the .h and the .c file.
  • Read in the data from the file. Data must be stored in a structure arrays.
  • Calculate the average grade for each subject.
  • Output all of the subject names and their average grades.
  • Answer the questions below the task.

Do it in the following order:

  1. Download the files, compile and run the program – make sure it works before modifying it and that you understand all of the code! You probably need to get the data file from last week.
  2. Declare a structure in the header file
    • Structures are typically declared in the headers, not in the code files (though possible).
    • Consider all of the fields necessary to put in the structure. Think of it this way: One structure will contain one entry from the file – subject’s name, number of grades and the grades given. If you have 2 subject, that makes for an array of size 2 etc.
    • Give the structure an alias so you can reduce the amount of code written. This means creating a typedef. You can combine declaration and type definition if you wish.
    • Usually we have one field in the struct for each field in the file.
  3. Declare an array based on that structure in the main().
  4. Update the code.¬†Don’t write any new functionality!
    1. Remove the unnecessary variable declarations for the fields that we now have inside of the structure.
    2. Alter the code to use the structures and their members.
    3. Don’t forget to update the functions and their prototypes (where are the prototypes?)
  5. Test the code with the original functionality, see that it works before proceeding.
  6. Add a new function CalculateAverageGrade() , which will calculate the average grade for each subject.
    1. This will require us to add another field for the struct declaration. Add that field to the struct so we can use it!
    2. Where should you add a prototype for this new function?
    3. Now write the function.
  7. Modify the OutputData()  function to also print the average grade.
  8. Test the updated code!
  9. Did you spot it? We left a bug in the reading file part! If not, it’s time to look for it now and fix it! (hint: it’s something to do with the file pointer and how files should be handled).

In the minireport, write the answers the following questions:

  1. What does the perror() function do? Try to make it execute and see what it prints!
  2. What was the mistake that we left in the code?
  3. What happens if instead of a number, you will get a string in place of the grade count? Does it crash or is it protected? How?
  4. What happens if one would do the same for a grade?
  5. What happens if the grade count is 0? Is your CalculateAverageGrade() function ready for that?
  6. What if the grade count is greater than MAX_GRADES ? Could that be simply guarded against as well?