Topic: AP Computer Science A | C++ Programming Your sports league uses the follo
ID: 3597259 • Letter: T
Question
Topic: AP Computer Science A | C++ Programming
Your sports league uses the following lottery system to select draft picks for the four worst teams in the league:
The last place team gets 20 balls in an urn.
The second-to-last place team gets 10 balls in the urn.
The third-to-last place team gets 6 balls in the urn
The fourth-to-last place team gets 4 balls in the urn.
To determine the first pick in the draft a ball is selected at random. The team owning that ball gets the first pick. The ball is then put back in the urn.
To determine the second pick in the draft a ball is selected at random. If the ball belongs to the team that got the first pick then it is put back in and the process repeats until a ball is selected that does not belong to the first pick.
To determine subsequent picks in the draft the process repeats until a ball is selected that belongs to a team that has not already been chosen.
Write a function that takes as input which of the four teams have already been granted picks, simulates selecting a ball from the urn according to the lottery rules, and returns the team that belongs to the selected ball. You get to choose how to design your function to perform these actions. Write a main function that outputs the draft order (e.g., a possible order is: second-to-last picks 1, last place picks 2, third-to-last picks 3, and fourth-to-last picks 4). If you change the random seed then the order should differ if you run the program multiple times.
For a slightly harder version of the problem, allow the user to input the names of the four teams. The program should then output the team names in the draft order.
Explanation / Answer
Here is the code for the problem stated(coded in C++):
//Program to select draft picks for the four worst teams in the league:
#include <iostream>
#include <string>
#include <cstdlib> // for random number generation
#include <ctime>
#include <cstdio>
#include <ctime>
#include <vector>
using namespace std;
// Write a main function that outputs the draft order (e.g., a possible order is:
// second-to-last picks 1, last place picks 2, third-to-last picks 3, and
//fourth-to-last picks 4). If you change the random seed then the order should
// differ if you run the program multiple times.
// function takes inputs the previous tems selected and returns a new team
string selectTeam(vector<string> previousTeamsSelected, vector<string> teams) {
srand(time(0));
get_ball:
long pick = (rand() % 40) + 1;
int teamSel = 0;
if (pick > 0 && pick < 21) {
teamSel = 0;
}
if (pick > 20 && pick < 31) {
teamSel = 1;
}
if (pick > 30 && pick < 37) {
teamSel = 2;
}
if (pick > 36 && pick < 41) {
teamSel = 3;
}
if (!previousTeamsSelected.empty()) {
for (int i = 0; i < previousTeamsSelected.size(); i++) {
if (previousTeamsSelected[i]==teams[teamSel]) {
goto get_ball;
}
}
return teams[teamSel];
} else {
return teams[teamSel];
}
}
int main()
{
string tempstring;
vector<string> teams,selectedTeams;
cout << "Enter the name of teams(worst team should be entered first):" << endl;
for (int i = 0; i < 4; i++) {
getline(cin, tempstring);
teams.push_back(tempstring);
}
string teamSelected;
for (int i = 0; i < 4; i++) {
teamSelected = selectTeam(selectedTeams,teams);
selectedTeams.push_back(teamSelected);
}
for (int i = 0; i < 4; i++) {
cout << selectedTeams[i] << " picks " << i+1 <<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.