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 taskc and placed in the toplevel 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 pedanticerrors o task taskc 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:
$ task classstudents.csv classactivitycsv total students absent students grade mean grade sd
Further examples of correct output to help you test your program are given in the taskexample.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 eg 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 nonzero 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 countlines 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 countlines 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 convertinteger 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 ie 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 toolmemcheck leakcheckyes showreachableyes trackfdsyes task classstudents.csv classactivitycsv
If your program has correctly handled dynamic memory allocation, the last line of output should read:
ERROR SUMMARY: errors from contexts suppressed: from
If you have closed all user file handles, you should see a line reading:
FILE DESCRIPTORS: open at exit.
ie only the standard streams are open at exit.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
