Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

Monty Hall code problem

$
0
0
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
Attached Files

Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images