-
October 21st, 2014, 10:03 PM
#1
Monty Hall code problem
When i use option 2 in my code it says that I win 100% of the time and lose 0% of the time for some reason, I thought the logic looked correct but i"m not sure where I went wrong. If someone could help me out that would be appreciated!
Heres my Code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int MontyHallGame(int b, int x);
int main()
{
srand((unsigned)time(NULL));
int option;
int count=0;
int wincount=0;
printf("Menu:\n\n");
printf("1.Game Mode\n");
printf("2.Research Mode\n");
printf("3.Exit\n");
scanf("%d", &option);
while (option!=3)
{
printf("\nGames played: %d\n", count);
printf("Games won: %d\n\n", wincount);
if (option==1)
{
int r=rand()%3;
if(r<3)
{
int b;
printf("Pick a door\n\n");
printf("1.Door 1\n");
printf("2.Door 2\n");
printf("3.Door 3\n");
scanf("%d", &b);
int x;
x=1;
printf("\nWould you like to switch doors?\n\n");
printf("1.Yes\n");
printf("2.No\n");
scanf("%d", &x);
int c;
int Prize;
c=MontyHallGame(b, x);
if(c==1)
{
wincount++;
count++;
printf("\nCongratulations, the prize is behind this door, you win!\n");
}
else
{
count++;
printf("\nThe door is empty, you lose!\n");
}
}
}
if (option==2)
{
int a=-100;
printf("Please select your method for the simulation\n\n");
printf("1.Always Stay\n");
printf("2.Always Switch\n");
scanf("%d", &a);
int g=0;
int wins=0;
int loses=0;
int N;
float i;
float j;
int b;
int Prize;
int x;
int Gameruns;
printf("\nPlease enter a value for N\n");
scanf("%d", &N);
for (Gameruns=0; Gameruns<N; Gameruns++)
if (a==1)
{
b=1;
g=MontyHallGame(b, x);
if (g=1)
wins++;
else
loses++;
}
if (a==2)
{
b<=3;
g=MontyHallGame(b, x);
if (g=1)
loses++;
else
wins++;
}
i=(wins/N)*100;
j=(loses/N)*100;
printf("\nYou won %f percent of the time and lost %f percent of the time.\n", i, j);
}
printf("Menu:\n\n");
printf("1.Game Mode\n");
printf("2.Research Mode\n");
printf("3.Exit\n");
scanf("%d", &option);
}
return 0;
}
int MontyHallGame(int b, int x)
{
int Prize;
int win;
Prize=rand()%3+1;
if (x==1)
{
if (b==Prize)
win=1;
else
win=0;
}
if (x==2)
{
if (b==Prize)
win=0;
else
win=1;
}
return win;
}
Lab 3.c
-
October 22nd, 2014, 04:57 AM
#2
Re: Monty Hall code problem
Are you sure you mean this as a statement? Its syntax is valid but it doesn't do anything!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 22nd, 2014, 06:18 AM
#3
Re: Monty Hall code problem
your monty hall code is flawed it doesn't do what it's supposed to do.
the point is
you pick one of 3 doors
monty then opens one of the 2 other doors that ISN'T the one with the prize
only after this happens you can either stick with your door or switch to the one other (unopened) door.
-
October 22nd, 2014, 07:19 AM
#4
Re: Monty Hall code problem
Code:
int r=rand()%3;
if(r<3)
There's no need for that if statement. r can only be 0, 1 or 2.
Why are you creating a random number there and in MontyHallGame?
Use variable names that mean something. a, b, c, etc. mean nothing to anybody reading your code. Makes it much harder to follow and debug.
As to your problem, look at this statement.
if (g=1)
This would be a good time to learn the debugger.
-
October 22nd, 2014, 12:43 PM
#5
Re: Monty Hall code problem
Originally Posted by GCDEF
Why are you creating a random number there and in MontyHallGame?
that should have been Obvious...
one for the user picking one of the 3 doors. (your 'there')
and one for which door the prize is behind (in MontyHallGame())
but this is missing the code for monty to select the door where the prize isn't behind.
which really also needs a random for monty to decide which one to open in case both other doors have no prize.
-
October 22nd, 2014, 12:56 PM
#6
Re: Monty Hall code problem
Originally Posted by OReubens
that should have been Obvious...
one for the user picking one of the 3 doors. (your 'there')
and one for which door the prize is behind (in MontyHallGame())
but this is missing the code for monty to select the door where the prize isn't behind.
which really also needs a random for monty to decide which one to open in case both other doors have no prize.
He asks the user to enter the door of their choice.
Code:
int b;
printf("Pick a door\n\n");
printf("1.Door 1\n");
printf("2.Door 2\n");
printf("3.Door 3\n");
scanf("%d", &b);
-
October 23rd, 2014, 07:53 AM
#7
Re: Monty Hall code problem
oh, didn't really look at the code for that. I assumed it was part of the automated mode where the computer is the user in a 'try it a bazillion times' type mode.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|