// This program calculates the average of a set of exams. // The user is prompted to enter the exam scores and to enter -99 // when there are no more scores. The average will be printed. #include <iostream.h> #include <iomanip.h> int main() { const int SENTINEL = -99; int grade; int numberOfGrades; int totalGrades; float averageGrade; // initialize the total and number of grades read in totalGrades = 0; numberOfGrades = 0; // read in the grades, total them, and count how many grades there are cout << "Enter the first exam score (enter -99 to end): "; cin >> grade; while (grade != SENTINEL) { totalGrades = totalGrades + grade; numberOfGrades = numberOfGrades + 1; cout << "Enter the next exam score (enter -99 to end): "; cin >> grade; } // calculate and print the average averageGrade = float(totalGrades) / numberOfGrades; cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); cout << setprecision(1); cout << "The average grade for the exam is : " << averageGrade << endl; }
Enter the first exam score (enter -99 to end): 95 Enter the next exam score (enter -99 to end): 85 Enter the next exam score (enter -99 to end): 75 Enter the next exam score (enter -99 to end): 90 Enter the next exam score (enter -99 to end): 80 Enter the next exam score (enter -99 to end): -99 The average grade for the exam is : 85.0
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002