Question

In: Computer Science

Using the following array: //may be declared outside of the main function const int NUM_Games =4;...

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

  • Write a function called getValidScore that 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();
  • Write a function called getValidGameNumber that allows a user to enter in an integer and loops until a valid number that is>= 1 and <= NUM_GAMES. It returns the valid value.
    • int getValidGame();
  • Write a function called displayScores that takes the score array as a parameter and displays the scores in the format in the sample run below.
    • void displayScores(int scores[NUM_GAMES]);
  • Write a function called ChangeAScore that 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]);
  • Write a function called displayGameHighestScore that takes the score array as a parameter, it displays the number of the game with the highest score. Note: array elements are 0 to 2 but the game numbers are 1 to 5.
    • //displays the number of the game with the highest score
    • void displayGameHighestScore(int scores[NUM_GAMES]);
  • Write a function called displaySortedScores that 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. You can use either bubble or selection sort.
    • void displaySortedScores(int scores[NUM_GAMES]);

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

Solutions

Expert Solution

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";
}


Related Solutions

#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int stateLength = 6; const int cityLength = 20; const int streetLength = 30; int custNomber = -1; // Structure struct Customers { long customerNumber; char name[nameLength]; char state[stateLength]; char city[cityLength]; char streetAddress1[streetLength]; char streetAddress2[streetLength]; char isDeleted = 'N'; char newLine = '\n'; int zipCode; }; int main() { ofstream file; file.open("Customers.dat", fstream::binary | fstream::out); char go; long entries = 0; struct Customers data; do...
Please answer these The following array is declared: int grades[20]; a. Write a printf() statement that...
Please answer these The following array is declared: int grades[20]; a. Write a printf() statement that can be used to display values of the first, third, and seventh elements of the array. b. Write a scanf() statement that can be used to enter values into the first, third, and seventh elements of the array. c. Write a for loop that can be used to enter values for the complete array. d. Write a for loop that can be used to...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
How to inhabit the int array num with random numbers using a function that you then...
How to inhabit the int array num with random numbers using a function that you then invoke from the main method. I already created a function that generates an array of 5 random integers. /* int i;   int arrayName[5];     for(i= 0;i < 6; i++ ){            //type arrayName[arraysize];            arrayName[i] = rand()% 200;       printf("array[%d] = %d\n",i,arrayName[i]); */ // use rand() % some_number to generate a random number int num[5]; int main(int argc, char **argv) { // hint // function invocation goes here...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Create a Visual Studio console project (c++) containing a main() program that declares a const int...
Create a Visual Studio console project (c++) containing a main() program that declares a const int NUM_VALUES denoting the array size. Then declare an int array with NUM_VALUES entries. Using a for loop, prompt for the values that are stored in the array as follows: "Enter NUM_VALUES integers separated by blanks:" , where NUM_VALUES is replaced with the array size. Then use another for loop to print the array entries in reverse order separated by blanks on a single line...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Can anyone translate this to flowgorith. #include<stdio.h> int main()    {    int seatingChart[10];       //array...
Can anyone translate this to flowgorith. #include<stdio.h> int main()    {    int seatingChart[10];       //array for seating chart       int fc = 0, ec = 5,i;       //starting positions for first class and economy class       printf("Welcome to the Airline Reservation System (ARS)\n\n");       for(i=0;i<10;i++)       //initializing the array with zero        {        seatingChart[i] = 0;        }    char choice;       do        {        int ch;   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT