Help, I can't get this program to work correctly. If I put in a number higher or lower than is allowed it gives me the correct error message. However, when I sum up the numbers it is adding them in the total. How do I get the code to not add in the invalid numbers.
Code:
#include <stdio.h>
int minimum (int grades[], int no_of_grades);
int maximum (int grades[], int no_of_grades);
int main()
{
int grades[10], i, min_grades = 0;
int max_grades = 0;
int sum = 0;
int no_of_grades = 0;
int avg = 0;
do
{
printf ("Enter the number of grades to process (0-10): ", no_of_grades);
scanf ("%i", &no_of_grades);
fflush (stdin);
if (no_of_grades < 1 || no_of_grades > 10)
printf ("Invalid number of grades.\n");
} while (no_of_grades < 1 || no_of_grades > 10);
for (i = 0; i < no_of_grades; i++ )
do
{
printf ("Enter grade for student #%i: ",i+1);
scanf ("%i", &grades[i]);
fflush(stdin);
if (grades[i] <= 0 || grades[i] >= 101)
printf ("***Invalid grade entered. Please enter 0 - 100.***");
sum = sum + grades[i];
} while (grades[i] <= 0 || grades[i] >= 101);
printf ("\nThe sum of all grades is %i",sum);
avg = sum / no_of_grades;
min_grades = minimum(grades, no_of_grades);
max_grades = maximum(grades, no_of_grades);
printf ("\nThe class average is %i",avg);
printf ("\nThe minimum grade is %i",min_grades);
printf ("\nThe maximum grade is %i",max_grades);
getchar();
} /*End Main*/
int minimum (int grades[], int no_of_grades)
{
int min_grades, i;
min_grades = grades[0];
for (i = 1; i < no_of_grades; ++i)
if (grades[i] < min_grades)
min_grades = grades[i];
return min_grades;
}
int maximum (int grades[], int no_of_grades)
{
int max_grades, i;
max_grades = grades[0];
for (i = 1; i < no_of_grades; ++i)
if (grades[i] > max_grades)
max_grades = grades[i];
return max_grades;
}