could you finish this code for me? I am suck A game begins by putting the prize
ID: 3545444 • Letter: C
Question
could you finish this code for me? I am suck
A game begins by putting the prize behind a door at random, then prompts the user to pick
a number from 1,2,3, which indicates the door the user picks
The program then decides a door to open. The door must be one behind which there is no prize and
which is not picked by the user. In case there are two such doors available, the program picks one at random.
The program next prompts the user to decide if he wants to switch. Based on the user's decision, the
program outputs if the user won the prize or not.
The loop over game plays nishes if the user picks an invalid door number (a number outside f1;2;3g)
at the beginning of the game. When the loop is over, the program outputs the total number of games that
have been played, the number of games in which the user has won, and the relative frequency of winning.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int user[3]={1,2,3};
int count=0;
int countWon=0;
int prize = 1 + rand() % 3;
int no prize;
while ( user == 1 )
{
printf("the prize is behind one of the doors ", rand());
printf(" please pick a door from 1, 2 or 3 ");
scanf("%d, &user")
if ( rand () != user )
{
int no prize = !rand();
printf(" the there is no prize behind the door number %d ", no prize);
}
count++;
countWon++;
}
printf(" the total number of %d games played ,count ");
printf(" the user won the total number of %d games , countWon");
return 0;
}
Explanation / Answer
Here is the answer you were looking for
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int removeADoor(int userChoice,int prizeDoor);
int main(void)
{
int totalCount=0;
int winCount=0;
int prizeDoor = 0;
int userChoice=0;
char ch;
printf("Welcome to Monty Hall Game ");
while (1)
{
prizeDoor = 1 + rand()%3;
printf("Please pick a door from 1, 2 or 3 (enter a value of more than 3 to stop the game) :");
scanf("%d",&userChoice);
if(userChoice !=1 && userChoice !=2 && userChoice !=3)
break;
int removedDoor = removeADoor(userChoice,prizeDoor);
printf("There is no prize behind the door %d ",removedDoor);
ReadChoice:
printf("Do you want to stick (y/n):");
getchar();
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
if(userChoice==prizeDoor)
{
winCount++;
printf("You got it right! ");
}
else
{
printf("You lost ");
}
else if(ch=='N'||ch=='n')
if(userChoice==prizeDoor)
{
printf("You lost ");
}
else
{
winCount++;
printf("You got it right! ");
}
else
{
printf("Enter a valid choice ");
goto ReadChoice;
}
totalCount++;
}
printf("The total of %d games played ",totalCount);
printf(" the user has won %d games ", winCount);
return 0;
}
int removeADoor(int userChoice,int prizeDoor)
{
if(userChoice != prizeDoor)
return 6 - (prizeDoor+userChoice);
else
{
int randomValue= rand()%2;
switch(userChoice)
{
case 1 :
if(randomValue)
return 2;
else
return 3;
break;
case 2:
if(randomValue)
return 1;
else
return 3;
break;
case 3:
if(randomValue)
return 1;
else
return 2;
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.