Question: Your task is to write a program to read in student grades from a file and calculate statistics for these grades. Your C source file

Your task is to write a program to read in student grades from a file and calculate statistics for these grades.
Your C source file for this task must be named task1.c and placed in the top-level directory of your git project for this coursework.
Details
Your program should read in a student CSV file and an activity CSV file and output the following statistics in the format detailed below:
total number of students
number of students absent from the activity
arithmetic mean grade for activity
population standard deviation of grades for activity
You are free to choose whichever method you want to implement your program with the following restrictions:
your implementation must contain a correctly declared main function
you are not allowed to use global variables
all dynamically allocated memory should be freed
all user file handles should be closed
You can use any standard library functions (including those in the C maths library). If using the maths library you should remember to link to the maths library when compiling your program:
$ gcc -Wall -ansi -pedantic-errors -o task1 task1.c -lm
Your program should take two command line arguments: the filename of the student CSV file and filename of the activity CSV file respectively. It should output the statistics detailed above in the format shown in the example below. All real numbers should be output to two decimal places. For example:
$ ./task1 class01_students.csv class01_activity01.csv total students =10 absent students =2 grade mean =44.00 grade sd =31.77
Further examples of correct output to help you test your program are given in the task1.example.log file. Your program will be tested against a number of randomly generated classes in the GitLab assessment pipeline. Your program will also be tested against a number of invalid cases e.g. an incorrect number of command line arguments, or incorrect numbers of columns in the student or activity CSV files. In these cases, an error message should be printed and your program should immediately exit with a non-zero exit code. All error messages in your implementation should be printed to stderr.
There are many ways to implement your program. I chose to use the following approach:
parse and sanity check the command line arguments:
sanity check the user has provided two command line arguments. sanity check the CSV files passed as command line arguments are readable files (using the access standard library function).
count the number of lines in the student CSV file. I wrote a count_lines function to do this. The total number of students is one less than this number.
count the number of lines in the activity CSV file (using the count_lines function again).
dynamically allocate an integer array to store the grades in the activity CSV file (of size one less than the number of lines in the activity CSV file).
loop through and read in each line (ignoring the header line) in the activity CSV file (using fgets):
for each line remove the line feed and carriage return (using strcspn). find the grade in the second field of each line (using strtok). convert the grade to an integer (I used the convert_integer function I wrote in previous courseworks) and store it in my dynamically allocated grade array.
close the activity CSV file
sum all the elements in the grade array and divide by the total number of students to calculate the mean grade.
loop through the grade array again to calculate the standard deviation (using the sqrt function in the calculation).
print out the statistics.
free the grade array.
Your program should handle dynamically allocated memory correctly i.e. free all dynamically allocated memory. It should also make sure all user file handles are closed before it exits. As discussed in lectures, a good tool for assessing if a program has handled dynamic memory allocation correctly is valgrind. Remember to compile your code with the -g flag to show line numbers in the valgrind output. To check your program using valgrind you can type:
$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --track-fds=yes ./task1 class01_students.csv class01_activity01.csv
If your program has correctly handled dynamic memory allocation, the last line of output should read:
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
If you have closed all user file handles, you should see a line reading:
FILE DESCRIPTORS: 3 open at exit.
i.e. only the 3 standard streams are open at exit.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!