// Mr. Minich
// CMPSC 201
// Ch. 11 Demo Program #3
// January 20, 2000
// Purpose - to illustrate the use of an array with a loop, accumulating a total.
#include <iostream>
using namespace std; int main() { int position = 0; int runningTotal = 0; int count = 0; int gameScores[101] = {0}; // it is assumed that the user will not need to enter more // than 100 game scores do { count++; // we are not going to make use of the // zero position of the array cout << "Enter the score of Game " << count << " (-99 to stop): "; cin >> gameScores[count];
if (gameScores[count] != -99) { runningTotal = runningTotal + gameScores[count]; }
} while(gameScores[count] != -99); // runningTotal is an accumulator. // The user enters the sentinel value of -99 to indicate that he/she // is finished entering game scores. cout << "Your average score was " << runningTotal / --count << endl;
// Do you know why the predecrementing operator is used with count in the
// statement above?
return 0; }// end of main