Using C++, Keeping track of your favorite team’s information is really hard, so
ID: 3844214 • Letter: U
Question
Using C++,
Keeping track of your favorite team’s information is really hard, so this program will help you. Write a program that stores a team of players’ points. You need the following two structures.
struct Player
name (string. We only record the player’s last name, so there won’t be a space in the name)
playerNumber(int, this represent the identification number for a player)
points (double, the points a player gained)
struct Team
teamName (string. This is your favorite’s team’s name. Use one word for the team’s name)
numOfTeamMembers (int, this will be from a user input and represent the number of players in a team)
ptrTeamMember (which is a pointer to the instance of Player structure. This pointer is used to dynamically allocate an array)
averagePoints (double, this represent the average of all players in a team.
There is no input validation needed for this assignment.
Functions:
void getScores(Team & team)
This function will
first ask user the team’s name
then ask user how many members are in the team (numOfTeamMembers),
and then dynamically allocate an array using “ptrTeamMember” with numOfTeamMembers.
The function should then ask user for name, playerNumber, point for each team member associated with each player in the team.
double calculateAverage(const Team & team)
This function will calculate and return the average points of the team. There shouldn’t be any cout statement in this function
void displayTeamInfo(const Team & team)
This function will display the team’s information. This function should first display a line of 50 stars (‘*’), and display the team’s name. The function should also display each team member’s information including their name (left justified, 50 spaces width), player number (left justified, 10 spaces), and points (left justified and 10 spaces).
The function should then display the average of all team member’s point.
Finally, the function should end with displaying a line of 50 stars.
void freeMemory(Team & team)
This function should free all memories dynamically allocated.
Main function should be no more than 5 lines with return 0 included
Team aTeam;
getScores(aTeam);
displayTeamInfo(aTeam);
freeMemory(aTeam);
Sample Output:
What is the team's name? Gryffindor
How many members in your team? 3
Please enter information for team member 1 :
Name: Ginny
Number: 1
Points: 100
Please enter information for team member 2 :
Name: Fred
Number: 2
Points: 350
Please enter information for team member 3 :
Name: Oliver
Number: 3
Points: 200
**************************************************
Gryffindor
Ginny 1 100
Fred 2 350
Oliver 3 200
**************************************************
216.67
void getScores(Team & team)
This function will
first ask user the team’s name
then ask user how many members are in the team (numOfTeamMembers),
and then dynamically allocate an array using “ptrTeamMember” with numOfTeamMembers.
The function should then ask user for name, playerNumber, point for each team member associated with each player in the team.
double calculateAverage(const Team & team)
This function will calculate and return the average points of the team. There shouldn’t be any cout statement in this function
void displayTeamInfo(const Team & team)
This function will display the team’s information. This function should first display a line of 50 stars (‘*’), and display the team’s name. The function should also display each team member’s information including their name (left justified, 50 spaces width), player number (left justified, 10 spaces), and points (left justified and 10 spaces).
The function should then display the average of all team member’s point.
Finally, the function should end with displaying a line of 50 stars.
void freeMemory(Team & team)
This function should free all memories dynamically allocated.
Main function should be no more than 5 lines with return 0 included
Team aTeam;
getScores(aTeam);
displayTeamInfo(aTeam);
freeMemory(aTeam);
Explanation / Answer
#include <iostream>
#include <cstring>
#include <iomanip>
#include <limits>
using namespace std;
struct Player
{
char name[50];
int number;
int points;
};
struct Team
{
char name[50];
int numOfPlayers;
Player *ptrTeamMember;
double averagePoints;
};
void getScore(Team &aTeam)
{
int points;
char playerName[50];
int num;
cout << "Enter Team name: ";
cin.get(aTeam.name, 50);
cout << "Enter Number of players in the Team: ";
cin >> aTeam.numOfPlayers;
aTeam.ptrTeamMember = new Player[aTeam.numOfPlayers];
for(int i = 0; i< aTeam.numOfPlayers; i++)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),' ');
cout << "Enter information of team member : "<< i+1 << " ";
cout << "Name: ";
cin.get(playerName, 50);
cout << "Number : ";
cin >> num;
cout << "Points: ";
cin >> points;
aTeam.ptrTeamMember[i];
strcpy(aTeam.ptrTeamMember[i].name,playerName);
aTeam.ptrTeamMember[i].number = num;
aTeam.ptrTeamMember[i].points = points;
}
}
double calculateAverage(const Team &team)
{
int points = 0;
for(int i = 0; i< team.numOfPlayers; i++)
{
points = points + team.ptrTeamMember[i].points;
}
return (double)points/team.numOfPlayers;
}
void displayTeamInfo(const Team &team)
{
cout<< "*********************************************************"<<endl;
cout << team.name <<endl;
for(int i = 0; i< team.numOfPlayers; i++)
{
cout << left << setw(50) << team.ptrTeamMember[i].name << left << setw(10) << team.ptrTeamMember[i].number << left << setw(10) << team.ptrTeamMember[i].points <<endl;
}
cout<< "*********************************************************"<<endl;
cout << team.averagePoints <<endl;
}
void freeMemory(Team &team)
{
delete team.ptrTeamMember;
}
int main()
{
Team aTeam;
getScore(aTeam);
aTeam.averagePoints = calculateAverage(aTeam);
displayTeamInfo(aTeam);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.