Question

In: Computer Science

C++ questions about Monty Hall Problem, please make sure to divide the program into functions which...

C++ questions about Monty Hall Problem, please make sure to divide the program into functions which perform each major task.

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.)

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
 
int randint(int n)
{
  return (1.0*n*std::rand())/(1.0+RAND_MAX);
}
 
int other(int doorA, int doorB)
{
  int doorC;
  if (doorA == doorB)
  {
    doorC = randint(2);
    if (doorC >= doorA)
      ++doorC;
  }
  else
  {
    for (doorC = 0; doorC == doorA || doorC == doorB; ++doorC)
    {
      // empty
    }
  }
  return doorC;
}
 
int check(int games, bool change)
{
  int win_count = 0;
  for (int game = 0; game < games; ++game)
  {
    int const winning_door = randint(3);
    int const original_choice = randint(3);
    int open_door = other(original_choice, winning_door);
 
    int const selected_door = change?
                                other(open_door, original_choice)
                              : original_choice;
 
    if (selected_door == winning_door)
      ++win_count;
  }
 
  return win_count;
}
 
int main()
{
  std::srand(std::time(0));
 
  int games = 10000;
  int wins_stay = check(games, false);
  int wins_change = check(games, true);
  cout << "staying: " << 100.0*wins_stay/games << "%, changing: " << 100.0*wins_change/games   << "%\n";
}

Related Solutions

C++ questions, Please make sure to divide your program into functions which perform each major task....
C++ questions, Please make sure to divide your program into functions which perform each major task. The game of rock paper scissors is a two player game in which each each player picks one of the three selections: rock, paper and scissors. The game is decided using the following logic: ROCK defeats SCISSORS (“smashes”) PAPER defeats ROCK (“wraps”) SCISSORS defeats PAPER (“slices”) If both players choose the same selection the game ends in a tie. Write a program that asks...
C++ questions, please make sure to dividet he program into functions which perform each major task,...
C++ questions, please make sure to dividet he program into functions which perform each major task, A leap year is defined as any calendar year which meets the following criteria: If the year is not divisible by 4, it is a common year If the year is not divisible by 100, it is a leap year If the year is not divisible by 400, it is a common year Otherwise it is a leap year For your program you will...
The Monty Hall problem is a famous problem loosely based on the game show Let's Make...
The Monty Hall problem is a famous problem loosely based on the game show Let's Make a Deal. You are a contestant on the game show. There are 3 doors in front of you. Behind one door is a prize, and behind the other two doors are goats. Assume the door with the prize is picked uniformly at random from the three doors. First, you pick a door. Then, Monty Hall will open one of the other two doors that...
The Monty Hall problem is named for its similarity to the Let's Make a Deal television...
The Monty Hall problem is named for its similarity to the Let's Make a Deal television game show hosted by Monty Hall. The problem is stated as follows. Assume that a room is equipped with three doors. Behind two are goats, and behind the third is a car. You are asked to pick a door, and will win whatever is behind it. Let's say you pick door 1. Before the door is opened, however, someone who knows wh at's behind...
Recall the Monty Hall problem as presented in class. There are three caves, two of which...
Recall the Monty Hall problem as presented in class. There are three caves, two of which contain dragons and one of which is a hiding princess. The prince chooses a cave and a wizard (truthfully) reveals to the prince that one of the other caves has a dragon. 2 The prince now has the option to switch to the other cave (the one he didn’t choose and wasn’t revealed to have a dragon), and try his luck finding the princess...
Recall the Monty Hall problem as presented in class. There are three caves, two of which...
Recall the Monty Hall problem as presented in class. There are three caves, two of which contain dragons and one of which is a hiding princess. The prince chooses a cave and a wizard (truthfully) reveals to the prince that one of the other caves has a dragon. 2 The prince now has the option to switch to the other cave (the one he didn’t choose and wasn’t revealed to have a dragon), and try his luck finding the princess...
(a) In the Monty Hall problem with 100 doors, you pick one and Monty opens 98...
(a) In the Monty Hall problem with 100 doors, you pick one and Monty opens 98 other doors with goats. What is the probability of winning (assuming you would rather have a car than a goat) if you switch to the remaining door? Explain your answer. (b) Suppose Monte opens 98 doors without checking for cars. What is the probability that, once the doors are open, changing your choice will not change your chances of winning.
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT