Answer to Question #188469 in C++ for Drake

Question #188469

You've to develop table tennis fantasy league.Here're details of league:no. of team (4) and no. of player/team (3). Each team will have to play

other team twice,so you can calculate total number of matches.

1.addPlayers():add player data.

Sr. No. Name Type Point Availability

1 Name Player 1 Attacking 35.93 true

3.assignRanks(): Assign ranking to 4 teams on the basis of random number.

4.teamSelection(): Form team on basis of strategy,defensive team will prefer two defensive

players and one attacking player.Whereas an attacking team will prefer two attacking players and

one defensive player.

6.generateMatchStats():This function will generate match status.Assign winning team players’ point

b/w 10 & 20 randomly. For losing team assign them point between 5 & 10 randomly.

7.bestPlayersInMatch():Display information of best player of both teams

8.UpdatePlayerPoints():After match update ranking point of players

9.printLeaderBoard():Print sorted list of player in all teams


1
Expert's answer
2021-05-07T03:38:39-0400
#include<iostream>
using namespace std;

class Player{
  
private:
string name;
string type; // attacking or defensive
double points; // Randomly assign points b/w 30 and 40
bool availability;
  
public:
  
Player(){
  
}
  
// Constructor
Player(string name, string type, int points, bool avail){
  
this->name = name;
this->type = type;
this->points = points;
availability = true;
}
  
string getName(){
return name;
}
  
double getPoints(){
  
return points;
}
  
string getType(){
return type;
}
};

class Team{
  
public:
string teamName;
int teamRank;
Player* teamPlayers;
  
  
void setRank(int rank){
teamRank = rank;
}
};

class Match{
  
public:
int matchNo;
Team homeTeam;
Team awayTeam;
  
Team *teams; // POinter type of Team class
  
// Keeps the count of particular type player
int count = 0;
  
Player* matchPlayers; // For total number of match players. in the question it is 15
  
int allPlayerSize; // Holds the user defined size
  
// Default constructor for default values   
Match(){
teams = new Team[4]; // create 4 teams
allPlayerSize = 15;
matchPlayers = new Player[allPlayerSize];
}
  
int length(){
return count;
}
  
void addPlayers(){
  
// Input the total number of players for the match
cout<<"how many players are total in the match? ";
cin>>allPlayerSize;
// Allocate the space for the input size players
matchPlayers = new Player[allPlayerSize];
  
string playerName;
string type;
double points;
bool avail;
  
for(int i = 0; i< allPlayerSize; i++){
  
printf("\n### Player %d #####\n", i+1);
cout<<"Enter name: ";
cin>>playerName;
cout<<"Enter type: ";
cin>>type;
// randomly generates floating points between 30 to 40
points = 30 + static_cast <float> (rand()) / ( static_cast <float> (RAND_MAX/(40-30)));
// create the objects for each player   
matchPlayers[i] = Player(playerName, type, points, true);
  
}
  
}
  
Player* returnPlayers(string type){
  
// Create an array of object of Player for storing the players of particular type   
static Player *players = new Player[allPlayerSize];

// Iterate through all the players and search the given type player
// Add the matched players in the players array   
for(int i = 0; i<allPlayerSize; i++){
  
if(matchPlayers[i].getType() == type)
players[count++] = matchPlayers[i];
}
// return the array of players of particular type   
return players;
}
  
// Function to randomly assign rank to each team
  
void assignRanks(){
  
int randRank = rand() % 4 + 1; // from 1 to 4
  
for(int i = 0; i<4; i++){
  
teams[i].setRank(randRank);
}
  
cout<<"\nRanks has been assigned!"<<endl;
}
};

// Main driver method

int main(){
  
Match match;
  
match.addPlayers();
  
string type;
  
cout<<"\nEnter player type to filter: ";
cin>>type;
  
Player *players = match.returnPlayers(type);
  
int size = sizeof(players) / sizeof(players[0]);
  
cout<<"\nList of players of type "<<type<<endl;
  
for(int i = 0; i<match.length(); i++){
  
cout<<"\nPlayer"<<i+1<<endl;
cout<<"Name: "<<players[i].getName()<<endl;
cout<<"Points: "<<players[i].getPoints()<<endl;
  
}
  
match.assignRanks();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS