// This program calculates the average of a set of exams. // The program will read in exactly 6 grades, then calculate and // print the average. #include <iostream.h> #include <iomanip.h> int main() { 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 while (numberOfGrades < 6) { cout << "Enter the next exam score: "; cin >> grade; totalGrades = totalGrades + grade; numberOfGrades = numberOfGrades + 1; } // calculate and print the average averageGrade = float(totalGrades) / numberOfGrades; cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint); cout << setprecision(1); cout << "The average grade for the exam is : " << averageGrade << endl; }
Enter the next exam score: 85 Enter the next exam score: 70 Enter the next exam score: 90 Enter the next exam score: 95 Enter the next exam score: 77 Enter the next exam score: 82 The average grade for the exam is : 83.2
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002