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

This is my code for an array using the bubblesort in the function outside of main....
This is my code for an array using the bubblesort in the function outside of main. My bubblesort is not working correctly , and I was told i'm missing a +j instead of +i in the function for void sorter.Can someone please help me with making my sorter work properly? everything else is fine and runs great. Thank you #include <stdio.h> #include <stdlib.h> #include <time.h> void printer(int *ptr, int length); void randomFill(int *ptr, int length); void sorter(int *ptr, int length);...
// function definitions go into hw08.cpp: // hw08.cpp namespace hw08 { int main(); const int ARRAY_SIZE...
// function definitions go into hw08.cpp: // hw08.cpp namespace hw08 { int main(); const int ARRAY_SIZE = 5; const int DYNAMIC_SIZE = 15; const int TIC_TAC_TOE_SIZE = 3; // function definitions: //------------------------------------------------------------------------------ int increment_value(int x) // pass a value, compute a new value by adding 5 to x and return it { // ... x += 5; // temp, replace when defining function return x;           // included so that incomplete lab code will compile } void increment_pointer(int* p)...
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> #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...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
#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++
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to a function) Pre condition array - a pointer to a an array of size element. pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order) Post condition Sort the array in ascending or descending based on the the pointer to a function. Call the function bubble_sort to sort the array in ascending...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT