I'm trying to write a program that will ask for 10 grades and return the average. If the grade is "-1", I want to ignore it and not compute the grade for it. The first part is working fine, but do you have any suggestions on how to ignore the negative grades?
The number of grades that count will be different from the number in the array:
1 2 3 4 5 6 7 8 9 10 11 12 13
double average(float listGPA[])
{
int sum = 0;
unsigned count = 0; // number of grades to count
for (int i = 0; i < SIZE; i++)
{
if (listGPA[i] >= 0) {
sum += listGPA[i];
++count;
}
}
return sum / count;
}
Oh smack me silly. Speaking of floating point exceptions, line 12 of my example does integer division instead of floating point. So it should be returnstatic_cast<double>(sum) / count;
That'll teach me to post code without testing it. :(