SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
More Flow Of Control
Exercise:
Programming Projects
Question:13 | ISBN:9780321531346 | Edition: 7

Question

The following problem is sometimes called “The Monty Hall Game Show Problem.” You are a contestant on a game show and have won a shot at the grand prize. Before you are three closed doors. Behind one door is a brand new car. Behind the other two doors are consolation prizes. The location of the prizes is randomly selected. The game show host asks you to select a door, and you pick one. However, before revealing the contents behind your door, the game show host reveals one of the other doors with a consolation prize. At this point, the game show host asks if you would like to stick with your original choice or switch your choice to the other closed door. What choice should you make to optimize your chances of winning the car? Does it matter whether you stick with your original choice or switch doors?

Write a simulation program to solve the game show problem. Your program should make 10,000 simulated runs through the problem, randomly selecting locations for the prize, and then counting the number times the car was won when sticking with the original choice, and counting the number of times the car was won when switching doors.

Output the estimated probability of winning for both strategies. Be sure that your program exactly simulates the process of selecting the door, revealing one, and then switching. Do not make assumptions about the actual solution (for example, simply assuming that there is a 1/3 or 1/2 chance of getting the prize).

Appendix 4 gives library functions for generating random numbers.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program code:

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
  srand(time(NULL));
  int winStay=0;
  int winSwitch=0;
  for(int trail=0;trail<10000;trail++)
  {
	int prize=rand()%3;
   int choice=rand()%3;
    int reveal;
     do
	 {
		 reveal=rand()%3;
	 }while((reveal==prize)||(reveal==choice));
	 int switchDoor;
	 for(int i=0;i<3;i++)
	 {
		 if((i!=reveal)&&(i!=choice))
			 switchDoor=i;
	 }
	 if(prize==choice)
		 winStay++;
	 if(prize==switchDoor)
	    winSwitch++;
  }
  cout<<"Wins when staying"<<winStay<<endl;
  cout<<"Win when switches."<<winSwitch<<endl;
  return 0;
} 

OUTPUT OF THE PROGRAM CODE:

Wins when staying 3330
Win when switches  .6670

 

0 0

Discussions

Post the discussion to improve the above solution.