In: Computer Science
Using the following array:
//may be declared outside of the main function
const int NUM_Games =4;
//may only be declared within the main function
int scores[NUM_GAMES] = {122, 76, 92, 143};
Write a C++ program to run a menu-driven program with the following choices:
1) Display the scores
2) Change a score
3) Display game with the highest score
4) Display a sorted list of the scores
5) Quit
Sample Run:
Welcome to the Gaming Program!
1) Display the scores
2) Change a score
3) Display game with the highest score
4) Display a sorted list of the scores to the menu
5) Quit
Select an option (1..4)..1
Display scores
Game 1 Game 2 Game 3 Game 4
122 76 92 143
1) Display the scores
2) Change a score
3) Display game with the highest score
4) Display a sorted list of the scores to the menu
5) Quit
Select an option (1..4)..2
Change a score
Please enter in the game number …
20
Please enter in a valid game number …
2
Please enter in the score ...
135
1) Display the scores
2) Change a score
3) Display game with the highest score
4) Display a sorted list of the scores to the menu
5) Quit
Select an option (1..4)..3
The game with the highest score is 4
Select an option (1..4)..4
Sorted list of scores
143 135 122 92
Select an option (1..4)..5
Screenshot of program code:-
Screenshot of output:-
Program code to copy:-
#include <iostream>
using namespace std;
const int NUM_GAMES =4;
// functions prototype
int getValidScore();
int getValidGame();
void displayScores(int scores[NUM_GAMES]);
void ChangeAScore(int scores[NUM_GAMES]);
void displayGameHighestScore(int scores[NUM_GAMES]);
void displaySortedScores(int scores[NUM_GAMES]);
int main()
{
int scores[NUM_GAMES] = {122, 76, 92, 143};
int option;
do
{
// Display menu
cout << "Welcome to the
Gaming Program!" << endl;
cout << "1) Display the
scores" << endl;
cout << "2) Change a score"
<< endl;
cout << "3) Display game with
the highest score" << endl;
cout << "4) Display a sorted
list of the scores" << endl;
cout << "5) Quit" <<
endl;
//Prompt & read choice selected
by the user
cout << "Select an option
(1..4)..";
cin >> option;
// Appropriate function will be
called based on user choice
switch(option)
{
case
1: // calling function to display the scores
displayScores(scores);
break;
case
2: // calling function to change the score
ChangeAScore(scores);
break;
case
3: // calling function to display the number of the
game with the highest score
displayGameHighestScore(scores);
break;
case
4: // calling function to display a sorted list of the
scores
displaySortedScores(scores);
break;
}
cout << endl;
}while(option!=5);
return 0;
}
// Function allows a user to enter in an integer and loops until
a valid number that is
// >= 0 and <= 150 is entered. It returns the valid
value.
int getValidScore()
{
int score;
cout << "Please enter in the score ..." <<
endl;
while(1)
{
// read score from user
cin >> score;
if(score>= 0 &&
score<= 150)
break;
else
cout <<
"Please enter in a valid score ..." << endl;
}
return score;
}
// Function allows a user to enter in an integer and loops until
a valid number i.e.
// >= 1 and <= NUM_GAMES. It returns the valid value.
int getValidGame()
{
int gameNum;
cout << "Please enter in the game number ..."
<< endl;
while(1)
{
// read game number from user
cin >> gameNum;
if(gameNum>= 1 &&
gameNum<= NUM_GAMES)
break;
else
cout <<
"Please enter in a valid game number ..." << endl;
}
return gameNum;
}
// Function takes the score array as a parameter and displays the
scores.
void displayScores(int scores[NUM_GAMES])
{
cout << "Display scores" << endl;
cout << "Game 1\tGame 2\tGame 3\tGame
4\n";
for(int i=0; i<NUM_GAMES; i++)
cout << scores[i] <<
"\t";
}
// Function takes the score array as a parameter, it allows the
user to
// enter in a valid score and a valid game, It then stores the
score under
// the selected game in the scores array.
void ChangeAScore(int scores[NUM_GAMES])
{
cout << "Change a score" << endl;
// calling function to get the valid game number
int gameNum = getValidGame();
// calling function to get the valid score
int score = getValidScore();
// stores the score under the selected game in the
scores array.
scores[gameNum-1] = score;
}
// Function takes the score array as a parameter, it displays
the number
// of the game with the highest score.
void displayGameHighestScore(int scores[NUM_GAMES])
{
int high = scores[0];
int gameNum = 1;
for(int i=1; i<NUM_GAMES; i++)
{
if(scores[i] > high)
{
high =
scores[i];
gameNum =
i+1;
}
}
// displays the number of the game with the highest
score
cout << "The game with the highest score is "
<< gameNum;
}
// Function takes the scores array as a parameter, it creates a
local copy of the scores array,
// sorts the new array in descending order and displays values in
the new array.
void displaySortedScores(int scores[NUM_GAMES])
{
// declare local copy of score array
int sortedScore[NUM_GAMES];
// creates a local copy of the scores array
for(int i=0; i<NUM_GAMES; i++)
sortedScore[i] = scores[i];
// sorts the new array in descending order
for (int i=0 ; i<NUM_GAMES-1; i++)
{
for (int j=0 ; j<NUM_GAMES-i-1; j++)
{
if (sortedScore[j]>
sortedScore[j+1])
{
int temp = sortedScore[j];
sortedScore[j] =
sortedScore[j+1];
sortedScore[j+1] = temp;
}
}
}
// displays values in the new array
cout << "Sorted list of scores" <<
endl;
for(int i=0; i<NUM_GAMES; i++)
cout << sortedScore[i]
<< "\t";
}