In: Computer Science
This program will store roster and rating information for a basketball team. Coaches rate players during tryouts to ensure a balanced team. A roster can include at most 10 players.
(1) Prompt the user to input five pairs of numbers: A player's
jersey number (0 - 99) and the player's rating (1 - 9). Store the
jersey numbers in one int array and the ratings in another int
array. Output these arrays (i.e., output the roster).
Ex:
(2) Implement a menu of options for a user to modify the roster.
Each option is represented by a single character. The program
initially outputs the menu, and outputs the menu after a user
chooses an option. The program ends when the user chooses the
option to Quit.
Ex:
(3) Implement the "Output roster" menu option.
Ex:
(4) Implement the "Add player" menu option. If the user chooses this option and the roster is full, print the following error message:
If the roster is not full, prompt the user for a new player's
jersey number and rating, and append the values to the two
arrays.
Ex:
(5) Implement the "Delete player" menu option. If the user chooses the option when the roster is empty, immediately print the message:
If the roster is not empty, prompt the user for a player's
jersey number. Remove the player from the roster (delete the jersey
number and rating), paying attention not to leave unused spaces in
the two arrays.
Ex:
If the given jersey number is not found, inform the user:
(6) Implement the "Update player rating" menu option. Prompt the
user for a player's jersey number. Prompt again for a new rating
for the player, and then look up and change that player's
rating.
Ex:
In this case, if the given jersey number is not found, no further action is taken (note that the format of the program requires both input either way).
(7) Implement the "Output players above a rating" menu option.
Prompt the user for a rating. Print the jersey number and rating
for all players with ratings above the entered value.
Ex:
If no players are found above a given rating, the program will simply produce an empty list.
BONUS
For an extra 10 points, implement a secret option s that prints the message:
and sorts the roster by jersey number. Do not add this option to the MENU message. And remember to move the player rating accordingly!
I need help with my HW its in C++. it also asking for an input. thank you!!
// C++ program to maintain a roster of basketball players
#include <iostream>
#include <iomanip>
#define MAX_PLAYERS 10 //maximum number of player in the roster
using namespace std;
// function declaration
void sort(int player_jersey[], int player_rating[], int num_players);
int main() {
// roster of players jersey and rating
int player_jersey[MAX_PLAYERS];
int player_rating[MAX_PLAYERS];
char choice;
int num_players = 0;
bool found;
int jersey, rating;
// loop to input details for 5 players
cout<<"Enter details of 5 players : "<<endl;
for(num_players=0;num_players<5;num_players++)
{
cout<<"Enter jersey number(0-99) and player's rating (1-9) separated by a space : ";
cin>>player_jersey[num_players]>>player_rating[num_players];
}
// loop that continues until the user exits
do
{
// display the menu
cout<<"\nMenu"<<endl;
cout<<"1. Output roster "<<endl;
cout<<"2. Add Player"<<endl;
cout<<"3. Delete Player"<<endl;
cout<<"4. Update Player Rating"<<endl;
cout<<"5. Output players above a rating"<<endl;
cout<<"6. Exit"<<endl;
cout<<"Enter your choice (1-6): ";
cin>>choice;
switch(choice)
{
case '1': // output the roster
if(num_players == 0)
cout<<"Empty Roster"<<endl;
else
{
cout<<left<<setw(15)<<"Jersey"<<left<<setw(10)<<"Rating"<<endl;
for(int i=0;i<num_players;i++)
cout<<left<<setw(15)<<player_jersey[i]<<left<<setw(10)<<player_rating[i]<<endl;
cout<<endl;
}
break;
case '2': // add a player
if(num_players == MAX_PLAYERS)
cout<<"Roster full, cannot add more players"<<endl;
else
{
cout<<"Enter new player's jersey number and rating (separated by a space) : ";
cin>>jersey>>rating;
player_jersey[num_players] = jersey;
player_rating[num_players] = rating;
num_players++;
}
break;
case '3': // delete a player
if(num_players == 0)
cout<<"Empty Roster, cannot delete a player"<<endl;
else
{
cout<<"Enter the player's jersey number : ";
cin>>jersey;
found = false;
for(int i=0;i<num_players;i++)
{
if(player_jersey[i] == jersey)
{
for(int j=i;j<num_players-1;j++)
{
player_jersey[j] = player_jersey[j+1];
player_rating[j] = player_rating[j+1];
}
found = true;
num_players--;
break;
}
}
if(!found)
cout<<"Player with jersey number : "<<jersey<<" not present in the roster"<<endl;
}
break;
case '4': // update the player rating
cout<<"Enter player's jersey number : ";
cin>>jersey;
cout<<"Enter new rating for the player : ";
cin>>rating;
for(int i=0;i<num_players;i++)
{
if(player_jersey[i] == jersey)
{
player_rating[i] =rating;
break;
}
}
break;
case '5': // output players above a rating
cout<<"Enter the rating : ";
cin>>rating;
for(int i=0;i<num_players;i++)
{
if(player_rating[i] > rating)
cout<<"Jersey : "<<player_jersey[i]<<" Rating : "<<player_rating[i]<<endl;
}
cout<<endl;
break;
case '6': // quit
break;
case 's': // secret option to sort players based on jersey numbers
sort(player_jersey,player_rating,num_players);
break;
default:
cout<<"Invalid choice"<<endl;
}
}while(choice != '6');
return 0;
}
// function to sort the players based on jersey numbers
void sort(int player_jersey[], int player_rating[], int num_players)
{
int min;
for(int i=0;i<num_players-1;i++)
{
min = i;
for(int j=i+1;j<num_players;j++)
{
if(player_jersey[j] < player_jersey[min])
min = j;
}
if(min != i)
{
int temp = player_jersey[i];
player_jersey[i] = player_jersey[min];
player_jersey[min] = temp;
temp = player_rating[i];
player_rating[i] = player_rating[min];
player_rating[min] = temp;
}
}
}
//end of program
Output: