// // Example 10.1. Program to read the number of golf scores // and then to read and print those scores // #include #include using namespace std; const int MAX_NUM_PLAYERS = 100; // maximum number of players int main() { int getNumPlayers(); int score[MAX_NUM_PLAYERS]; // array of scores int player; // index for array int numPlayers; // actual number of players numPlayers = getNumPlayers(); // read values for array score cout << endl << "Please enter " << numPlayers << " scores " << "with blanks separating scores:" << endl; for (player = 0; player < numPlayers; ++player) cin >> score[player]; // Print the scores cout << endl << "\tGolf Scores for Round:" << endl << endl; cout << "\tPlayer #\tScore" << endl; cout << "\t________\t_____" << endl; for (player = 0; player < numPlayers; ++player) cout << "\t" << setw(4) << player + 1 << "\t\t" << setw(4) << score[player] << endl; return 0; } // // Function to read and return number of players // Pre: none // Post: The number of players, an integer between // 1 and MAX_NUM_PLAYERS, was returned. // int getNumPlayers() { int numPlayers; // actual number of players cout << "How many players are in the golf tournament? "; cin >> numPlayers; while ((numPlayers <= 0) || (numPlayers > MAX_NUM_PLAYERS)) { cout << "The number of players must be between 1 and " << MAX_NUM_PLAYERS << endl; cout << "Please enter the number of players again. "; cin >> numPlayers; } return numPlayers; }