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>
Mathematics 6 1 2 2 4 5 3
Databases 5 3 4 4 3 5
Physics 8 2 2 3 3 4 3 5 4
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:
- 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.
- 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!).
- Declare an array based on that structure in the main().
- Update the code. Don’t write any new functionality!
- Remove the unnecessary variable declarations for the fields that we now have inside of the structure.
- Alter the code to use the structures and their members.
- Don’t forget to update the functions and their prototypes (where are the prototypes?)
- Test the code with the original functionality, see that it works before proceeding.
- 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?
- Modify the OutputData() function to also print the average grade.
- 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?