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:

  • Read in the data from the file. Data must be stored in structure arrays.
  • Calculate the average grade for each subject.
  • Output all of the subject names and their average grades.

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.
    • 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.
    • When read from the file, structures often contain all of the fields in the entry
    • Consider adding any necessary fields to the structure (think what you will need in the future!).
  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. I hope you foresaw what field or fields should be added to the structure. If not, you can add it now. Where should you add a prototype for this?
  7. Modify the OutputData()  function to also print the average grade.
  8. Test the updated code!

When showing your code, make sure you know the answers to the following questions:

  • What does the perror() function do? Try to make execute and see what it prints!
  • Were all the resources freed after the code finished? Was the input file closed? If not, fix it!
  • 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?
  • What happens if one would do the same for a grade?
  • What happens if the grade count is 0? Is your CalculateAverageGrade() function ready for that?
  • What if the grade count is greater than MAX_GRADES ? Could that be simply guarded against as well?