In: Computer Science
In C, For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th array position to hold the computed average. Once the file has been read and the averages calculated and stored in the array, you must sort the list by grade in descending order. Finally, the records should be printed to screen in descending final grade order with statistics at the end of the file for the class average, highest and lowest grades.
Requirements: Your program should make use of the following functions. 1. A function to read the file into an array of arrays. This function should take an empty multi-dimensional array from the caller. The function should ask the user for the name of the file. You may assume the file name entered will be accurate and accessible. You may omit this requirement and hard code the path to the file. You will be given a test file (shown below). I suggest hard coding the filename while you develop your project to save typing it over and over while you test. 2. A function to compute the grade for each array/record. This function should access the array and average the test grades, inserting the final average into the last position for each record as a float. 3. A function to sort the array of arrays by final grade in descending order (highest grade first). This function should take the entire array. 4. A function to print out the results of the operation to screen calculating the required totals and formatting the output appropriately. This function should take the entire array as well. Your program is expected to only deal with fractional values. Therefore, you are required to use a multi-dimensional array of type float or double. Your program will not have to resize your array; you may hard code the size of the array using a constant (#define). The test files will have 10 records/students, a student number followed by 4 grades. You may not use any global variables save for the use of a constant for array size if you wish as shown often in some examples.
output:
Sample Output: Student Id Test 1 Test 2 Test 3 Test 4 Final ******************************************************************
7899 92 90 88 86 89
6814 85 86 92 88 87.75
8234 77 87 84 98 86.5
7654 76 87 84 88 83.75
3534 86 81 84 73 81
7284 56 81 87 98 80.5
7234 76 81 84 78 79.75
7934 76 91 84 65 79
9901 45 78 79 80 70.5
6465 87 54 68 72 70.25
******************************************************************
File:Print out sorted grades Load Data from file Compute Grade Sort Records Main/Compute Grades.c
Input File (unsorted) grades.txt
Student Id Test 1 Test 2 Test 3 Test 4
6814 85 86 92 88
7234 76 81 84 78
6465 87 54 68 72
7899 92 90 88 86
9901 45 78 79 80
8234 77 87 84 98
7934 76 91 84 65
7284 56 81 87 98
7654 76 87 84 88
3534 86 81 84 73
c code:
#include <stdio.h>
#include <stdlib.h>
//get the inputs from file
void inputs(float line[][6])
{
FILE* fin;
char filename[50];
int i=0;
//get the file name
printf("Enter the input file name: ");
scanf("%s",&filename);
fin =fopen(filename,"r"); //open the file in read mode
//if not file exits
if(!fin)
{
printf("\nCan not open input file\n");
exit(1);
}
//read the file
else
{
while(!feof(fin) ) //if not end of file read character
{
fscanf(fin,"%f%f%f%f%f",&line[i][0],&line[i][1],&line[i][2],&line[i][3],&line[i][4]);
i++;
}
}
}
//calculate the grade
void grade(float line[][6])
{
float total =0;
for(int i=0;i<10;i++)
{
total =0;
total= line[i][1]+line[i][2]+line[i][3]+line[i][4];
line[i][5] =total/4; //average of all marks
}
}
//sort the grade in descending order
void sort(float line[][6])
{
float tmp[6];
//check the order and sort
for(int i=0;i<10;i++){
for(int j=i+1;j<10;j++)
{
if(line[i][5]<line[j][5]){
for (int k = 0; k <6 ; k++)
tmp[k] = line[i][k];
for (int k = 0; k < 6; k++)
line[i][k] = line[j][k];
for (int k = 0; k < 6; k++)
line[j][k] = tmp[k];
}
}
}
}
//print the id marks and grade
void print(float line[][6])
{
printf("\n\nStudent Id \tTest 1 \tTest 2 \tTest 3 \tTest 4
\tFinal");
printf("\n
******************************************************************");
for(int i=0;i<10;i++)
printf("\n
%.f\t\t%.f\t%.f\t%.f\t%.f\t%.2f",line[i][0],line[i][1],line[i][2],line[i][3],line[i][4],line[i][5]);
printf("\n ******************************************************************\n\n");
}
int main()
{
float line[10][6];
//call all the functions
inputs(line);
grade(line);
sort(line);
print(line);
return 0;
}
output:

//for any clarification please do comments. if you found this solution useful, please give me thumbs up. thank you.