Why does my program stop here? Is it creating an infinite loop or something? It doesn’t print the highest grade and lowest grade and the general weighted average. It only shows it when I type ‘exit.’ I use Dev-C++.
int grades[5], i, lGrade, hGrade;
float sum = 0, GWA;
printf("Enter 5 grades in percentile format within 0-100.");
for (i = 0; i < 5; i++) {
if (i == 0) {
printf("\n");
}
printf("\tGrade No. %d => ", i + 1);
scanf("%d", &grades[i]);
if ((grades[i] < 0) || (grades[i] > 100)) {
printf("\n\tOnly enter grades from 0 to 100. Please try again.\n\n");
i -= 1;
} else {
continue;
}
}
for (i = 0; i < 5; i++) {
scanf("%d", &grades[i]);
sum += grades[i];
}
GWA = sum / 5;
lGrade = grades[0];
hGrade = grades[0];
for (i = 0; i < 5; i++) {
if (grades[i] < lGrade)
lGrade = grades[i];
else if (grades[i] > hGrade)
hGrade = grades[i];
}
printf("\nThe highest grade is %d.", hGrade);
printf("\nThe lowest grade is %d.", lGrade);
printf("\nThe general weighted average is %0.2f.", GWA);
>Solution :
Your code asks for the grades twice, it’s halting at the scanf here:
for (i = 0; i < 5; i++) {
scanf("%d", &grades[i]);
sum += grades[i];
}
Perhaps you meant printf() not scanf().
