Build a function with the prototype that randomly assigns th

Build a function with the prototype that randomly assigns the value `C\' or `G\' to the three reference parameters. C++

Your program should have a loop that runs 10000 times. In each iteration, randomly pick a door number between 1 and 3 for placing the car. Place goats behind the other doors. Simulate the player with a random door pick. Randomly pick a door not the player\'s choice that has a goat (there may be one or two of them depending on what the player picked), simulating Monty\'s choice. Increment a counter for strategy 1 if the player wins by switching from their choice to the other door that is still unopened after Monty\'s reveal of a goat. Increment a counter for strategy 2 if the player wins by sticking with the original choice. When the loop is finished, display the percentage of times the user wins by using each strategy along with a message that answers the question: \"is it to the player\'s advantage to switch doors?\"

Solution

// C++ code

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <iomanip> // std::setprecision
#include <math.h>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

void setupDoors(char &door1, char &door2, char &door3)
{
   int x = (rand() % 3)+1;
   if(x == 1)
   {
       door1 = \'C\';
       door2 = \'G\';
       door3 = \'G\';
   }
   else if(x == 2)
   {
       door1 = \'G\';
       door2 = \'C\';
       door3 = \'G\';
   }
   else
   {
       door1 = \'G\';
       door2 = \'G\';
       door3 = \'C\';
   }  
}

void doorChoicePick(char door1, char door2, char door3, int &player_Door, int &monty_Door, int& door_Car)
{
   player_Door = (rand() % 3)+1;

   if (door1 == \'C\')
       door_Car = 1;
   else if(door2 == \'C\')
       door_Car = 2;
   else
       door_Car = 3;

   if ((door_Car == 1 || door_Car == 2) && (player_Door == 1 || player_Door == 2))
       monty_Door = 3;
   else if ((door_Car == 1 || door_Car == 3) && (player_Door == 1 || player_Door == 3))
       monty_Door = 2;
   else
       monty_Door = 1;
}

int main()
{
   srand(time(NULL));
   int player_Door, monty_Door, door_Car,iterations = 10000;;
   char door1, door2, door3;
   double win = 0;
   for(int i = 0; i < iterations; i++)
   {
       setupDoors(door1, door2, door3);
       doorChoicePick(door1, door2, door3, player_Door, monty_Door, door_Car);

       if(player_Door == door_Car)
           win++;
   }
   double door_Same = (win / iterations) * 100;
   double otherDoor = (iterations - win) / iterations * 100;
   cout << \"Chances of winning when players sticks with same door: \" << (win / iterations) * 100 << \"%\" << endl;
   cout << \"Chances of winning when players chooses the other door: \" << (iterations - win) / iterations * 100 << \"%\" << endl;

   if (door_Same > otherDoor)
       cout << \"Player\'s advantage not to switch doors after Monty tells them\" << endl;
   else
       cout << \"Player\'s advantage to switch doors after Monty tells them\" << endl;

   return 0;
}

/*
output:

Chances of winning when players sticks with same door: 33.09%
Chances of winning when players chooses the other door: 66.91%
Player\'s advantage to switch doors after Monty tells them


*/

Build a function with the prototype that randomly assigns the value `C\' or `G\' to the three reference parameters. C++ Your program should have a loop that run
Build a function with the prototype that randomly assigns the value `C\' or `G\' to the three reference parameters. C++ Your program should have a loop that run

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site