Thread: Monty Hall Problem

  1. #31
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Quote Originally Posted by tabstop View Post
    Well, you've gotten through "A game begins by putting the prize behind a door at random", and "prompts the user to pick a number from 1;2;3" (more or less -- your "in-range" check is somewhat backwards). That means next must be "The program then decides a door to open."
    That is the part I am confused about, deciding which door to open.

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by tysawyers13 View Post
    That is the part I am confused about, deciding which door to open.
    Well, they tell you how; quoting again: "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."
    You know which door they picked (once you ask them) and you know which door has the prize (because you picked it). So: how can you tell whether there are two doors available, or just one? How do you know which one(s) are left?

  3. #33
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Okay, so I still do not understand how to tell if there are two doors available or how to get the program to open a door but this is what I have

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int game()
    
    
    {
        int choice, i;
        door[i]=0;
        printf("Would you like to switch your door? 1 for yes 2 for no.\n");
        scanf("%d" , &choice);
    }
    
    
    int main(void)
    {
        int door[3]={0, 0, 0};
        int x,prize, randomnumber, originaldoor, choice;
        prize=1;
    
    
        while (1)
        {
        door[randomnumber] = 1;
    
    
        printf("Which door do you pick 1, 2, or 3? 0 to exit.\n");
        scanf("%d", &originaldoor);
        game();
        
        if ((0>=originaldoor) || (originaldoor>3))
        {
            break;
        }
    
    
    
    
        }
    
    
    return(0);
    }

  4. #34
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Again I have basic knowledge of programming, so this is very difficult for me and I know that this code is incorrect.

  5. #35
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by tysawyers13 View Post
    Okay, so I still do not understand how to tell if there are two doors available or how to get the program to open a door but this is what I have
    That's not a programming question; that's a you-yourself question. You picked which door has the prize behind it. I told you what door I wanted. How do you yourself know whether there are two doors available to open or just one? Not the computer; if you are running the show, how do you know whether there's a choice of two doors?

  6. #36
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Quote Originally Posted by tabstop View Post
    That's not a programming question; that's a you-yourself question. You picked which door has the prize behind it. I told you what door I wanted. How do you yourself know whether there are two doors available to open or just one? Not the computer; if you are running the show, how do you know whether there's a choice of two doors?
    Okay, so if you have the prize in the original door you picked there would be 2 doors to open?

  7. #37
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by tysawyers13 View Post
    Okay, so if you have the prize in the original door you picked there would be 2 doors to open?
    Sounds like a plan! As a bonus, you've even already written the code for it, basically.

  8. #38
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Okay, now i just don't know how to open a random door..

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by tysawyers13 View Post
    Okay, now i just don't know how to open a random door..
    Well, that would be the same way you randomly picked a door to put the prize behind; but you seem to have skipped over that part too. I'm pretty sure I've seen people mention rand in this thread already; you should look up what the function does (type "man rand" into your computer or your search engine) to see how to use it.

  10. #40
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Quote Originally Posted by tabstop View Post
    Well, that would be the same way you randomly picked a door to put the prize behind; but you seem to have skipped over that part too. I'm pretty sure I've seen people mention rand in this thread already; you should look up what the function does (type "man rand" into your computer or your search engine) to see how to use it.
    I was told that "door[randomnumber] = 1; " would randomly put the prize behind the door..

  11. #41
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by tysawyers13 View Post
    I was told that "door[randomnumber] = 1; " would randomly put the prize behind the door..
    Tim meant that once you have generated your random number, then you can use that random number as the index of your array; the line "door[randomnumber]=1" is fine for marking that door as the door, but it doesn't actually generate the random number.

  12. #42
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    This is what I have now, I am still working on the final part of the loop:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int game()
    
    
    {
        int door[3]={0,0,0};
        int choice, i, newdoor, originaldoor, openeddoor, randomnumber;
        if (originaldoor=1);
        {
         openeddoor = rand() % 3+1;
    
    
        }
        printf("Door %d does not have the prize\n", openeddoor);
        printf("Would you like to switch your door? 1 for yes 2 for no.\n");
        scanf("%d" , &choice);
        while ((choice<0) || (3<choice))
        {
            printf("You entered an invalid number\n");
            break;
        }
        if
            (choice=1)
        {
            {
            newdoor=choice;
            }
        if
            (choice=0)
            newdoor=originaldoor;
        }
        if (newdoor=1);
        {
            printf("You have won the prize!\n");
        }
        if (newdoor=0);
        {
            printf("You lose!\n");
        }
    
    
    
    
    
    
    }
    int main(void)
    {
        int door[3]={0, 0, 0};
        int x, prize, randomnumber, originaldoor, choice, newdoor, y;
        prize=1;
    
    
        while (1)
        {
        srand (time(NULL));
        originaldoor = rand() % 3 + 1;
    
    
        door[randomnumber] = 1;
    
    
        printf("Which door do you pick 1, 2, or 3? 0 to exit.\n");
        scanf("%d", &originaldoor);
        game(originaldoor);
    
    
        if ((0>=originaldoor) || (originaldoor>3))
        {
            break;
        }
    
    
        }
    
    
    return(0);
    }

  13. #43
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    I am pretty sure there is still a problem with the random number though

  14. #44
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Several of your if statements are using the assignment (Single = sign) operator as opposed to the equality comparison operator (double == sign).

  15. #45
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    I have fixed that but it is still not working properly

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int game()
    
    
    {
        int door[3]={0,0,0};
        int choice, i, newdoor, originaldoor, openeddoor, randomnumber;
        if (originaldoor==1)
        {
         openeddoor==rand() % 3;
        }
        else
        {
            openeddoor==0;
        }
        printf("Door %d does not have the prize\n", openeddoor);
        printf("Would you like to switch your door? 1 for yes 2 for no.\n");
        scanf("%d" , &choice);
    
    
        while ((choice<0) || (2<choice))
    
    
        {
            printf("You entered an invalid number\n");
            break;
        }
        if
            (choice==1)
        {
            {
            newdoor=choice;
            }
        if
            (choice==2)
            newdoor==originaldoor;
        }
        if (newdoor==1)
    
    
        {
            printf("You have won the prize!\n");
        }
        else
        {
            printf("You lose!\n");
        }
    
    
    
    
    
    
    
    
    }
    int main(void)
    {
        int door[3]={0, 0, 0};
        int x, prize, randomnumber, originaldoor, choice, newdoor, y;
        prize==1;
    
    
        while (1)
        {
        srand (time(NULL));
        originaldoor == rand() % 3 + 1;
    
    
        door[randomnumber] == 1;
    
    
        printf("Which door do you pick 1, 2, or 3? 0 to exit.\n");
        scanf("%d", &originaldoor);
        game(originaldoor);
    
    
        if ((0>=originaldoor) || (originaldoor>3))
        {
            break;
        }
    
    
        }
    
    
    return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help with "Monty Hall" simulator program.
    By jsokol7 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2011, 11:54 PM
  2. MONTY HALL problem
    By jackalope in forum C Programming
    Replies: 10
    Last Post: 10-28-2010, 09:43 PM
  3. Replies: 1
    Last Post: 12-18-2007, 12:39 AM
  4. Monty Python
    By Stoned_Coder in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-18-2001, 01:29 PM

Tags for this Thread