//Fig 2.9: exercise //Class average program with sentinel-controlled repetition //eg. clear counter add to counter average in the end #include #include int main() { int total, //sum gradeCounter, //number of grades entered grade; //one grade float average; //number with decimal point for average //This is the initialization phase total = 0; gradeCounter = 0; //all totals and previous counters are reset to zero in memory //processing phase cout << "Enter grade, -1 to end: "; cin >> grade; while ( grade != -1 ) { total = total + grade; gradeCounter = gradeCounter + 1; cout << "Enter grade, -1 to end: "; cin >> grade; } //termination phase if ( gradeCounter != 0 ) { average = static_cast< float > ( total ) / gradeCounter; cout << "Class average is " << setprecision ( 2 ) << setiosflags ( ios::fixed | ios::showpoint ) << average << endl; } else cout << "No grades were entered" << endl; return 0; //Indicate that program successfully ended } // This shows an add and average out operation to records with no set entries //-1 indicates that no more numbers are to be entered and the average out the sum