// this program reads in grades, prints the grades, and calculates and // prints the average grade. the user can enter as many grades as // he or she wishes. #include <iostream.h> #include <iomanip.h> void readGrades(int [],int&); void printGrades(int [],int); float calcAvgGrade(int[],int); int main() { int grades[30]; int count; // set up output to print floating point values with one decimal place cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); cout << setprecision(1); readGrades(grades,count); printGrades(grades,count); cout << endl; cout << "The average grade is : " << calcAvgGrade(grades,count) << endl; } // read in the grades, stop reading when 0 is entered, and return // count of grades read in void readGrades(int grades[],int& count) { count = 0; cout << "Enter grade (enter 0 to end): "; cin >> grades[0]; while (grades[count] != 0) { count++; cout << "Enter grade (enter 0 to end): "; cin >> grades[count]; } } // calculate and return the average grade float calcAvgGrade(int grades[],int count) { int sum; float average; sum = 0; for (int i = 0 ; i < count ; i++) sum = sum + grades[i]; average = float(sum) / count; return average; } // print the grades void printGrades(int grades[],int count) { cout << endl << endl; cout << "The grades are: " << endl; for (int i = 0 ; i < count ; i++) cout << setw(7) << grades[i] << endl; }
Enter grade (enter 0 to end): 88 Enter grade (enter 0 to end): 71 Enter grade (enter 0 to end): 76 Enter grade (enter 0 to end): 83 Enter grade (enter 0 to end): 92 Enter grade (enter 0 to end): 85 Enter grade (enter 0 to end): 94 Enter grade (enter 0 to end): 0 The grades are: 88 71 76 83 92 85 94 The average grade is : 84.1
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002