140k views
2 votes
Write a program in C to create a program that will read students and grades from a file. The file contains first name, last name, and five grades. For each input record, display the last name followed by a comma followed by the first name followed by the student's average grade. After all records are processed, display the highest overall grade average?

User Mandelbug
by
7.6k points

1 Answer

6 votes

Final answer:

A C program can be written to read student names and grades from a file, calculate their average grades, and identify the highest overall grade average among them. You would use file handling to open and read the file, loops for processing each record, and store the highest average in a variable for display after all records have been processed.

Step-by-step explanation:

Creating a Student Grade Reader Program in C

To read students' first names, last names, and five grades from a file, and then calculate their average grades, a C program can be developed. This program will involve opening and reading the contents of a file, processing each student's grades to calculate their average, and displaying the information formatted as 'LastName, FirstName: AverageGrade'. In addition, after processing all records, the highest overall grade average will be displayed. Below is a simplified structure of what the program might look like:

#include

int main() {
FILE *file;
char firstName[50], lastName[50];
float grades[5], average, highestAverage = 0;

// Open the file
file = fopen("students.txt", "r");
if (file == NULL) {
printf("Error opening file\\");
return 1;
}

// Read records from the file
while (fscanf(file, "%s %s %f %f %f %f %f", &firstName, &lastName, &grades[0], &grades[1], &grades[2], &grades[3], &grades[4]) == 7) {
average = (grades[0] + grades[1] + grades[2] + grades[3] + grades[4]) / 5;
printf("%s, %s: %.2f\\", lastName, firstName, average);
if (average > highestAverage) {
highestAverage = average;
}
}
printf("Highest Average: %.2f\\", highestAverage);

// Close the file
fclose(file);
return 0;
}

Remember to handle file opening and reading errors, and to always close the file after processing.

User Aboutstudy
by
9.2k points