// // Example 10.4 a. Program to read golf scores from the file "ex1004.dat" // (no white space after last score) and then to print the winning score // #include #include #include const int MAX_NUM_PLAYERS = 100; // maximum number of players int main(void) { void ReadScoresFile(int [], int&); // prototypes int winner(const int [], int); int TourneyScore[MAX_NUM_PLAYERS], // array of scores TourneyNum; // actual number of players ReadScoresFile(TourneyScore, TourneyNum); cout << "The winning score is " << winner(TourneyScore, TourneyNum) << ".\n"; return 0; } // // Function to read values for an array // Pre: ex1004.dat is a file of golf scores. // Post: score is a constant integer array of golf scores. // NumPlayers is the number of golf scores. // void ReadScoresFile(int score[], int& NumPlayers) { ifstream GolfFile("ex1004.dat"); assert(GolfFile); // read values for array score until end of file NumPlayers = 0; while ((NumPlayers < MAX_NUM_PLAYERS) && (GolfFile >> score[NumPlayers])) NumPlayers++; // print error message if too much data for array if (!GolfFile.eof()) cout << "\nFile contained more than " << MAX_NUM_PLAYERS << " scores.\n" << "Only the first " << MAX_NUM_PLAYERS << " scores will be processed.\n"; GolfFile.close(); } // // Example 10.4a. Function to return the winning (minimum) // score of a golf tournament // Pre: score is a nonempty constant array of integer golf scores. // NumPlayers is the number of values in score. // Post: The winning score was returned. // int winner(const int score[], int NumPlayers) { int min; // ongoing minimum in array min = score[0]; for (int player = 1; player < NumPlayers; player++) if (score[player] < min) min = score[player]; return min; }