Imagine yourself on the set of a game show. You\'re given the choice of three do
ID: 3681301 • Letter: I
Question
Imagine yourself on the set of a game show. You're given the choice of three doors. Behind one of the doors is a car you can drive home in if you guess correctly. Behind the other two doors are goats. After you initially guess the door, the host of the game show (who knows the door holding the car) opens one of the other doors revealing a goat. He gives you the choice: stick with your original door, or choose the other unopened door. This problem is reminiscent of a Sixties game show hosted by an individual named Monty Hall. For this assignment you will write a program simulating this game and the results of either choice. You will ask the user how many times they wish to run the simulation. If the user enters 1000 for instance, your program will run through 1000 simulations where the player sticks with the original door and 1000 simulations where the player chooses the other unopened door. After the program is completed execution, the winning percentages will then be displayed for each strategy. (Be sure to make the initial selection, Monty's selection, and the second-chance selection happen randomly.) Be sure to divide your program into functions which perform each major task. (The purpose of this assignment is to find out whether or not it's to the player's advantage to change their selection.) please use C++ to do this problem
Explanation / Answer
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#define GOAT1 1
#define GOAT2 2
#define CAR 3
using namespace std;
vector<int> choice(){
vector<int> vec;
int random = rand()%(3-1+1) + 1; //generates random number between 1 and 3
vec.push_back(random);
if(random==1 || random==2){
vec.push_back((random==1)?2:1);
vec.push_back(3);
}
else{
int selection = rand()%(2-1+1) + 1; //generates random number between 1 and 2
vec.push_back(selection);
vec.push_back((selection==1)?2:1);
}
return vec;
}
int originalWinCount(int n){
int i,original_win=0;
for(i=0;i<n;++i){
vector<int> selections=choice();
int original_choice= selections[0];
if(original_choice==3)
original_win++;
}
return original_win;
}
int secondChanceWinCount(int n){
int i,second_chance_win=0;
for(i=0;i<n;++i){
vector<int> selections=choice();
int second_chance=selections[2];
if(second_chance==3)
second_chance_win++;
}
return second_chance_win;
}
void simulation(int n){
int original_win=0,second_chance_win=0;
original_win=originalWinCount(n);
second_chance_win=secondChanceWinCount(n);
cout<<"Win percentage for original choice: "<<(double)original_win/(double)n<<endl;
cout<<"Win percentage for second chance : "<<(double)second_chance_win/(double)n<<endl;
}
int main(){
int num;
cout<<"Number of times you want to run the simulation :"<<endl;
cin>>num;
simulation(num);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.