-
Notifications
You must be signed in to change notification settings - Fork 0
/
MontyHall.cpp
139 lines (102 loc) · 2.85 KB
/
MontyHall.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Master change
$$$$$$$$$$$$$$$$$$
Develop change
$$$$$$$$$$$$$$$$$$
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef int Door;
const int SIMULATION_COUNT = 100;
/**
* Suggested problem decomposition.
* You do not have to use these function declarations.
*/
//Function Declarations
Door hideCar();
Door openDoor(Door firstChoiceDoor, Door carBehindDoor);
Door makeFirstChoice();
Door makeSecondChoice(Door firstDoor, Door openedDoor);
void simulate(int sequence, int& win1, int& win2);
void printHeader();
/**
* Main
*/
int main()
{
int win1 = 0, win2 = 0;
srand(time(NULL)); // seed the random number generator
printHeader();
// Run the simulations.
for (int i = 1; i <= SIMULATION_COUNT; i++) simulate(i, win1, win2);
cout << endl;
cout << setw(4) << win1 << " wins if stay with the first choice" << endl;
cout << setw(4) << win2 << " wins if switch to the second choice" << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
cout << endl;
cout << "Win ratio of switch over stay: ";
cout << static_cast<double>(win2)/win1 << endl;
}
/***** Complete this program. ****/
//Function Definitions
Door hideCar()
{
Door carBehindDoor = rand()%3 + 1;
return carBehindDoor;
}
Door makeFirstChoice()
{
Door firstChoiceDoor = rand()%3 + 1;
return firstChoiceDoor;
}
Door openDoor(Door firstChoiceDoor, Door carBehindDoor)
{
Door openedDoor;
Door tempDoor = rand()%3 + 1;
while((firstChoiceDoor == tempDoor)||(carBehindDoor == tempDoor))
{
tempDoor = rand()%3 + 1;
}
openedDoor = tempDoor;
return openedDoor;
}
Door makeSecondChoice(Door firstDoor, Door openedDoor)
{
Door secondChoice;
Door tempDoor = rand()%3 + 1;
while((openedDoor == tempDoor)||(firstDoor == tempDoor))
{
tempDoor = rand()%3 + 1;
}
secondChoice = tempDoor;
return secondChoice;
}
void simulate(int sequence, int& win1, int& win2)
{
Door carBehindDoor = hideCar();
Door firstChoiceDoor = makeFirstChoice();
Door openedDoor = openDoor(firstChoiceDoor,carBehindDoor);
Door secondChoice = makeSecondChoice(firstChoiceDoor,openedDoor);
string w1 ="";
string w2 ="";
if (firstChoiceDoor== carBehindDoor)
{
win1++;
w1 = "Yes";
}
else if (secondChoice == carBehindDoor)
{
win2++;
w2 = "Yes";
}
cout << sequence<<"\t"<< carBehindDoor <<"\t" <<firstChoiceDoor<<"\t"<<openedDoor<<"\t"<<secondChoice<<"\t"<<w1<<"\t"<<w2<<"\t"<<endl;
}
void printHeader()
{
cout << "#""\t"<< "Car""\t" <<"First""\t"<<"Opened""\t"<<"Second""\t"<<"Win""\t"<<"Win""\t"<<endl;
cout << " ""\t"<< "Here""\t" <<"Choice""\t"<<"Door""\t"<<"Choice""\t"<<"First""\t"<<"Second""\t"<<endl;
}