-
Issue with Sum code
I have a program where I am trying to sum grades that are input by the end user. They can put in a max of 10 grades. The grades must be between 1-100. If they put in a number above or below they get an error message. When I run the program my "sum" command is adding in the grades that are in error. I was hoping someone could give me help?????
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;
}
-
Your problem is that your code executes the sum = sum + grades[i]; part even when the score is bad. You should print your error message if the score is bad, otherwise add it to the sum.
BTW, there is a separate C forum that is for questions in the C language.
-
first, you are using C, so this should be in the other forum.
this code doesnt look right:
Code:
if (grades[i] <= 0 || grades[i] >= 101)
printf ("***Invalid grade entered. Please enter 0 - 100.***");
sum = sum + grades[i];
you are saying to enter 0-100, however it will only accept > 0 and < 101, so the first part should be < 0 not <=. also, i think your sum statement should be in an 'else' clause. if the grade isnt acceptable, you shouldnt add it to the sum, only when the grade IS acceptable, ie in an else clause.
now some side notes:
- notice 'avg' is an integer so it would be more accurate to use some floating-point data type
- your lack of {}s (block statements) in your for loops is no problem in this program, but i would bet a large sum of money that in the future if you continue to do that you will be pulling your hair out at least once trying to find an error when the problem was that you had more than one statement in your for loop and you forgot to add {}s
-
Apart from using fflush(stdin), see the FAQ, and some fairly poor code indentation, what seems to be the problem?
> When I run the program my "sum" command is adding in the grades that are in error
OIC, consider using 'else'