Complementary task for topic: 2
M Nemeth · 2023-08-29 15:21:04.610220'
Loops: do/while: complex task
Loops: do/while: complex task
Task: Track student grades and calculate class statistics:
Write a C program that allows the user to enter the grades of multiple students. The program should prompt the user to enter the number of students and the grades for each student. It should then calculate and display the average grade, minimum grade, and maximum grade for each student, as well as the overall class average, minimum grade, and maximum grade.
Your program should output the results in the following format:
Student 1:
- Grade 1: X
- Grade 2: Y
- ...
- Average Grade: Z
- Minimum Grade: A
- Maximum Grade: B
Student 2:
- Grade 1: X
- Grade 2: Y
- ...
- Average Grade: Z
- Minimum Grade: A
- Maximum Grade: B
...
Class Average: X
Class Minimum Grade: Y
Class Maximum Grade: Z
Hint:
Solution
#include
int main() {
int numStudents;
printf("Enter the number of students: ");
scanf("%d", &numStudents);
if (numStudents <= 0) {
printf("Invalid number of students. Please enter a positive integer.\n");
return 0;
}
int student = 1;
int classTotalGrade = 0;
int classMinimumGrade = 100;
int classMaximumGrade = 0;
while (student <= numStudents) {
printf("\nStudent %d:\n", student);
int numGrades;
do {
printf("Enter the number of grades for Student %d: ", student);
scanf("%d", &numGrades);
if (numGrades < 0) {
printf("Invalid number of grades. Please enter a non-negative integer.\n");
}
} while (numGrades < 0);
if (numGrades > 0) {
int gradeNumber = 1;
int totalStudentGrade = 0;
int minimumGrade = 100;
int maximumGrade = 0;
while (gradeNumber <= numGrades) {
int grade;
printf("Enter Grade %d for Student %d: ", gradeNumber, student);
scanf("%d", &grade);
if (grade < 0 || grade > 100) {
printf("Invalid grade. Please enter a grade between 0 and 100.\n");
continue;
}
totalStudentGrade += grade;
if (grade < minimumGrade) {
minimumGrade = grade;
}
if (grade > maximumGrade) {
maximumGrade = grade;
}
gradeNumber++;
}
double averageGrade = (double)totalStudentGrade / numGrades;
printf("Average Grade: %.2f\n", averageGrade);
printf("Minimum Grade: %d\n", minimumGrade);
printf("Maximum Grade: %d\n", maximumGrade);
classTotalGrade += totalStudentGrade;
if (minimumGrade < classMinimumGrade) {
classMinimumGrade = minimumGrade;
}
if (maximumGrade > classMaximumGrade) {
classMaximumGrade = maximumGrade;
}
} else {
printf("No grades entered.\n");
}
student++;
}
double classAverage = (double)classTotalGrade / numStudents;
printf("\nClass Average: %.2f\n", classAverage);
printf("Class Minimum Grade: %d\n", classMinimumGrade);
printf("Class Maximum Grade: %d\n", classMaximumGrade);
return 0;
}
Explanation
In this example, the program prompts the user to enter the number of students and stores it in the numStudents variable. It then performs input validation to ensure a valid number of students. For each student, it prompts the user to enter the number of grades and performs input validation to ensure a non-negative integer. If a valid number of grades is entered, it proceeds to prompt the user to enter each grade, perform input validation for each grade to ensure it falls within the range of 0 to 100, and calculates the total grade, minimum grade, and maximum grade for the student. After all the grades for each student have been entered, the program calculates the average grade, minimum grade, and maximum grade for each student and displays them using printf(). It also keeps track of the total grade for the class, the minimum grade across all students, and the maximum grade across all students.