i got this code where i try to help the teacher to calculate the grades of the students bases on given tasks :
#include <stdio.h>
#define PASS 55
#define MAX_NUM_OF_STUDENTS 10
#define NUM_OF_TASKS 4
int main() {
int i, j, num_of_students, grade;
float highestGrade = 0, lowestGrade = 100, passedCount = 0;
float taskPercentages[NUM_OF_TASKS] = {0}; // Array to store task percentages
float totalGrades[MAX_NUM_OF_STUDENTS] = {0}; // Array to store total grades
printf("Welcome to our grading system!n");
// Take the percentage of each task and check for invalid input.
for (i = 0; i < NUM_OF_TASKS; i++) {
while (1) {
printf("Please enter the percentage of task %d:n", i + 1);
if (scanf("%f", &taskPercentages[i]) != 1) {
printf("Input Errorn");
} else if (taskPercentages[i] < 0 || taskPercentages[i] > 1) {
printf("Maximum percentage is 1 and minimum percentage is 0. Please try again!n");
} else {
break;
}
}
}
// Calculate the sum of task percentages.
float sumWeight = 0.0;
for (i = 0; i < NUM_OF_TASKS; i++) {
sumWeight += taskPercentages[i];
}
// Ensure the sum weight of the tasks is exactly 1.
if (sumWeight != 1.0) {
printf("The sum weight of the tasks is %.2f and it should be 1. Please try again!n", sumWeight);
return 1;
}
printf("How many students in your class?n");
while (1) {
if (scanf("%d", &num_of_students) != 1) {
printf("Input Errorn");
} else if (num_of_students < 1 || num_of_students > MAX_NUM_OF_STUDENTS) {
printf("Maximum number of students in a course is %d and minimum 1. Please try again!n", MAX_NUM_OF_STUDENTS);
} else {
break;
}
}
// Get students' grades and calculate the total grade.
for (i = 0; i < num_of_students; i++) {
totalGrades[i] = 0.0;
printf("Please enter student %d grades.n", i + 1);
for (j = 0; j < NUM_OF_TASKS; j++) {
while (1) {
printf("Enter grade %d:n", j + 1);
if (scanf("%d", &grade) != 1) {
printf("Input Errorn");
} else if (grade < 0 || grade > 100) {
printf("Maximum grade is 100, minimum grade is 0. Please try again!n");
} else {
totalGrades[i] += grade * taskPercentages[j];
break;
}
}
}
if (totalGrades[i] > highestGrade) highestGrade = totalGrades[i];
if (totalGrades[i] < lowestGrade) lowestGrade = totalGrades[i];
if (totalGrades[i] >= PASS) passedCount++;
}
float sumGrades = 0.0;
for (i = 0; i < num_of_students; i++) {
sumGrades += totalGrades[i];
}
float averageGrade = sumGrades / num_of_students;
printf("Number of students: %dn", num_of_students);
printf("Percentage of each task:n");
for (i = 0; i < NUM_OF_TASKS; i++) {
printf("Task %d: %.2fn", i + 1, taskPercentages[i]);
}
printf("nThe grades are:n");
for (i = 0; i < num_of_students; i++) {
printf("Student %d: %.2fn", i + 1, totalGrades[i]);
}
printf("nHighest grade: %.2fn", highestGrade);
printf("Lowest grade: %.2fn", lowestGrade);
printf("Average: %.2fn", averageGrade);
printf("Number of passing students: %.0fn", passedCount);
printf("Thank you for using our system!n");
return 0;
}
the output i am supposed to get in this specific input :
0.5 0 0 0.6 -1 v
is :
Welcome to our grading system!
Please enter the percentage of task 1:
Please enter the percentage of task 2:
Please enter the percentage of task 3:
Please enter the percentage of task 4:
The sum weight of the tasks is 1.10 and it should be 1. Please try again!
Please enter the percentage of task 1:
Maximum percentage is 1 and minimum percentage is 0. Please try again!
Input Error!
but my output is coming as :
Welcome to our grading system!
Please enter the percentage of task 1:
Please enter the percentage of task 2:
Please enter the percentage of task 3:
Please enter the percentage of task 4:
The sum weight of the tasks is 1.10 and it should be 1. Please try again!
my output dosen’t allow the user to give new input , whenever i try to fix it and combine them in the same loop i get runtime error .
any recommendations ?