Complementary task for topic: 9
M Nemeth · 2023-08-29 15:21:04.631218'
File Handling: read tabular
File Handling: read tabular
Write a C program that reads a text file named "students.txt" containing information about students. Each line in the file represents a student and is formatted as follows: "Name Age Grade".
The program should read the file, calculate the average age and average grade of the students, and write the results to a new text file named "averages.txt". Additionally, the program should identify the student with the highest grade and the student with the lowest grade and write their names to the "averages.txt" file.
Students.txt:
John 20 85
Emily 19 92
Michael 21 78
Sophia 22 88
averages.txt should be:
Average Age: 20.5
Average Grade: 85.75
Highest Grade: Emily
Lowest Grade: Michael
Hint: You need a struct for data to hold, like:
typedef struct {
char name[50];
int age;
int grade;
} Student;
Do not forget to typecast befor integer division!
Solution
#include
#include
typedef struct {
char name[50];
int age;
int grade;
} Student;
int main() {
FILE *inputFile, *outputFile;
char name[50];
int age, grade;
int totalStudents = 0;
int totalAge = 0;
int totalGrade = 0;
int highestGrade = 0;
int lowestGrade = 100;
char highestName[50];
char lowestName[50];
// Open the input file in read mode
inputFile = fopen("students.txt", "r");
// Check if the file was opened successfully
if (inputFile == NULL) {
printf("Error opening the input file.\n");
return 1;
}
// Read information about students from the input file
while (fscanf(inputFile, "%s %d %d", name, &age, &grade) != EOF) {
totalStudents++;
totalAge += age;
totalGrade += grade;
// Check if this student has the highest grade
if (grade > highestGrade) {
highestGrade = grade;
strcpy(highestName, name);
}
// Check if this student has the lowest grade
if (grade < lowestGrade) {
lowestGrade = grade;
strcpy(lowestName, name);
}
}
// Close the input file
fclose(inputFile);
// Calculate the average age and average grade
float averageAge = (float)totalAge / totalStudents;
float averageGrade = (float)totalGrade / totalStudents;
// Open the output file in write mode
outputFile = fopen("averages.txt", "w");
// Check if the file was opened successfully
if (outputFile == NULL) {
printf("Error opening the output file.\n");
return 1;
}
// Write the results to the output file
fprintf(outputFile, "Average Age: %.2f\n", averageAge);
fprintf(outputFile, "Average Grade: %.2f\n", averageGrade);
fprintf(outputFile, "Highest Grade: %s\n", highestName);
fprintf(outputFile, "Lowest Grade: %s\n", lowestName);
// Close the output file
fclose(outputFile);
printf("Results have been written to averages.txt.\n");
return 0;
}
Explanation
We define a structure Student to store information about each student, including their name, age, and grade. We declare the main function with a return type of int. We declare variables to handle the input and output files (inputFile and outputFile) and variables to store information about each student (name, age, and grade). We initialize variables to keep track of the total number of students (totalStudents), the total age of all students (totalAge), the total grade of all students (totalGrade), the highest grade (highestGrade), the lowest grade (lowestGrade), and the names of the students with the highest and lowest grades (highestName and lowestName). We open the input file in read mode using fopen and check if the file was opened successfully. We use a loop to read information about each student from the input file using fscanf. In each iteration of the loop, we update the total age, total grade, and check if the current student has the highest or lowest grade. After reading all the students' information, we close the input file using fclose. We calculate the average age and average grade of the students. We open the output file in write mode using fopen and check if the file was opened successfully. We write the results (average age, average grade, highest grade student's name, and lowest grade student's name) to the output file using fprintf. After writing all the results, we close the output file using fclose. The program prints a message to the console to indicate that the results have been written to the "averages.txt" file.