A competition takes place for the development of a new functionality that will b
ID: 3708754 • Letter: A
Question
A competition takes place for the development of a new functionality that will be added on the NBA daily stats. This new functionality will show the player with the maximum ratio of points per minute. Develop a program in C++ that prompts the user to enter data for a number of NBA players and then outputs the player(s) with the maximum ratio of points per minute. Store the players entered by the user in a dynamic array of structs.
Sample:
How many players will you enter? 2
Enter first name: Stephen
Enter last name: Curry
Enter points: 30.5
Enter minutes: 37.8
Enter first name: Derrick
Enter last name: Rose
Enter points: 23.5
Enter minutes: 34.8
The maximum ratio of points per minute is: 0.81
Players with maximum ratio of points per minute: Stephen Curry
-You must store the data entered by the user in an array of structs.
-Data for each player include: first name, last name, points scored in a game (double), and minutes spent in game (double).
-The number of players for which the program needs to work is 10. You must declare a constant integer in the global space and use this throughout your program.
-You can assume that each of the data entered by the user will not contain any spaces.
-You don't have to check if user input is invalid. The program will be tested for valid inputs.
-The program must stop prompting for further input if the maximum number of players is reached.
-In the example above, the maximum ratio of points per minute is shown with two decimal points. You are free to choose if you want to output the results with a specific number of decimal points or not.
-Your program must have at least these three functions:
o Function to read the players' data into an array
o Function to find the maximum ratio of points per minute (points scored in a game divided by minutes spent in game)
o Function to print the names of the players with the maximum ratio
-The program must stop prompting for further input once the maximum number of players is reached.
-The program must free the allocated memory before termination.
-Prompt the user for the number of players they would like to enter.
-Dynamically allocate an array of structs of that size.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <iomanip>
using namespace std;
struct player
{
string firstName;
string lastName;
double points;
double minutes;
};
void inputPlayers(player *players, int n);
double calculateMaxRatio(player *players, int n);
void printMaxPlayers(player *players, int n, double max);
int main()
{
player *players;
int n;
cout << "How many players will you enter? ";
cin >> n;
players = new player[n];
inputPlayers(players, n);
double max = calculateMaxRatio(players, n);
cout << fixed << setprecision(2); //display with 2 decimal places
printMaxPlayers(players, n, max);
delete []players;
return 0;
}
void inputPlayers(player *players, int n)
{
for(int i = 0; i < n; i++)
{
cout << "Enter first name: ";
cin >> players[i].firstName;
cout << "Enter last name: ";
cin >> players[i].lastName;
cout << "Enter points: ";
cin >> players[i].points;
cout << "Enter minutes: ";
cin >> players[i].minutes;
cout << endl;
}
}
double calculateMaxRatio(player *players, int n)
{
double max = 0;
double ratio;
for(int i = 0; i < n; i++)
{
ratio = players[i].points / players[i].minutes;
if(ratio > max)
max = ratio;
}
return max;
}
void printMaxPlayers(player *players, int n, double max)
{
cout << "The maximum ratio of pointers per minute is: " << max << endl;
cout << "Players with maximum ratio of points per minute:" << endl;
for(int i =0; i < n; i ++)
{
if(players[i].points / players[i].minutes == max)
cout << players[i].firstName << " " << players[i].lastName << endl;
}
}
output
-------
How many players will you enter? 2
Enter first name: Stephen
Enter last name: Curry
Enter points: 30.5
Enter minutes: 37.8
Enter first name: Derrick
Enter last name: Rose
Enter points: 23.5
Enter minutes: 34.8
The maximum ratio of pointers per minute is: 0.81
Players with maximum ratio of points per minute:
Stephen Curry
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.