Programming in C

(Barry) #1

68 Chapter 6 Making Decisions


if ( grade < 65 )
++failureCount;
}

average = (float) gradeTotal / numberOfGrades;

printf ("\nGrade average = %.2f\n", average);
printf ("Number of failures = %i\n", failureCount);

return 0;
}

Program 6.2 Output
How many grades will you be entering? 7
Enter grade #1: 93
Enter grade #2: 63
Enter grade #3: 87
Enter grade #4: 65
Enter grade #5: 62
Enter grade #6: 88
Enter grade #7: 76

Grade average = 76.29
Number of failures = 2

The variablegradeTotal, which is used to keep a cumulative total of the grades as they
are typed in at the terminal, is initially set to 0 .The number of failing test grades is
stored in the variable failureCount, whose value also is initially set to 0 .The variable
averageis declared to be of type floatbecause the average of a set of integers is not
necessarily an integer itself.
The program then asks the user to enter the number of grades that will be keyed in
and stores the value that is entered in the variable numberOfGrades.A loop is then set up
that will be executed for each grade.The first part of the loop prompts the user to enter
in the grade.The value that is entered is stored in the variable called, appropriately
enough,grade.
The value of gradeis then added into gradeTotal, after which a test is made to see
if it is a failing test grade. If it is, the value of failureCountis incremented by 1.The
entire loop is then repeated for the next grade in the list.
When all of the grades have been entered and totaled, the program then calculates the
grade average. On impulse, it seems that a statement such as
average = gradeTotal / numberOfGrades;

Program 6.2 Continued
Free download pdf