Thread: Monty Hall Problem

  1. #46
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Did you just do a search and replace? Cause now you have several variables being "assigned" with the comparison operator.

    Code:
    int d;
    
    d = 5; /* assignment */
    
    if (d == 5) ; /* comparison */
    You need to fix all these mistakes first. Secondly you still have places where your braces are all wrong. Lines 33-41 are all misaligned and the braces are not right. Heres how that part should look:

    Code:
        if (choice==1)
            newdoor = choice;
        else if (choice==2)
            newdoor = originaldoor;
    
        if (newdoor==1)
            printf("You have won the prize!\n");
        else
            printf("You lose!\n");
    Also, your "game" function reaches it's end without returning a value, yet it is declared to return an int (despite the fact you don't use the return value in the main() function). You also need to use "game(void)" in your declaration to be standards compliant.

    You should be listening to the warnings your compiler is telling you and fixing EVERY one. Also make sure all warnings are turned up to the MAX. You are definitely getting warnings if not errors.

  2. #47
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Okay so i have fixed all those errors but I still do not get correct results. I also did not get any errors or warnings for my earlier code.

  3. #48
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Post your current code.

  4. #49
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void 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;
        else 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));
        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);
    }

  5. #50
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Look at your game function. You don't have any parameters in your definition, yet you pass in an integer in main.

    You might want to make your game function like so..
    Code:
    void game(int originaldoor)
    Lose the local variable originaldoor in your function, you want to check the value passed in from main, not a local variable.

  6. #51
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Yes that makes a lot of sense, don't know why I did that.. But now when I run the program I always get the result of winning? Is there something wrong with my random number generator then?

  7. #52
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Actually i get that if you change your door you always win.

  8. #53
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Sebastiani View Post
    This is the inherent problem with probability, or rather, our misuse of it as such. The information that it encodes has neither a dimension of causality nor a basis in actuality. It simply tells us what can possibly happen, not what is going to happen or why what does end up happening does happen in the first place. In other words, there is a definite limit to it's applicability to real-world situations.
    Probability tells you what will happen if the experiment is repeated many times. Quantum physics seems rather dependent on it.
    Apparently, most mathematicians who've chimed in on this matter seem to have either missed or ignored this distinction, so the consensus by majority rule is in favor of Ms. Savant's position. But there are nonetheless many of us out there who remain unconvinced.
    I don't see why people have a problem understanding the probability of this, although I've heard it was quite confusing even to some mathematicians.


    When Monty opens one of the unchosen doors to show it empty and asks if you want to switch, it's exactly the same as if he said, without opening a door, "You can either keep the door you've chosen or you can have BOTH of the other doors."


    Clearly choosing two doors instead of one gives you twice the chance of winning (2/3 compared to 1/3).


    The fact that at least one of them must be empty and whether or not he opens an empty one doesn't enter into it (except to mislead people).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #54
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    How would I get the program to find the only remaining door if the prize is in the door you choose before the first door is opened?

  10. #55
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by tysawyers13 View Post
    Is there something wrong with my random number generator then?

    Yes you are calling srand() each iteration of your while() loop, you only need to call that once at the beginning of the program (in main)

  11. #56
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Quote Originally Posted by nonpuz View Post
    Yes you are calling srand() each iteration of your while() loop, you only need to call that once at the beginning of the program (in main)
    Yes i have changed that, but now I get a winning answer an unlikely amount of times?

  12. #57
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    In the posted code above, you don't actually assign the random number you generate to the variable 'randomnumber'. You call rand() and then the value just gets thrown away. Also, I'm not sure if this was mentioned before, but you're +1 in the rand() call is wrong. Your array is of size 3, that means the highest index is 2, so rand() % 3 is all you need; that will give you values in the range 0, 1, 2, which is the indexes for your array of size 3.

    Another point is that your "winning door" is ALWAYS door 1, regardless of where you put the prize. You need to change the if check to make it check the selected prize door, not just 2 every time. Once you assign the value to 'randomnumber' you just use that variable to index which door is the 'prize' door in your comparison later. This means you probably have to pass the winning door number to your door check function, as well as the door the user has chosen.

  13. #58
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    So I must change the last if statement? I thought that if new door would be equal to 1 then it should be true?\
    Code:
    if (choice==1)
            newdoor = choice;
        else if (choice==2)
            newdoor = originaldoor;
        if (newdoor==0)
            printf("You lose!\n");
        else
            printf("You have won the prize!\n");

  14. #59
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    I mean if new door was equal to 0 then you lost.

  15. #60
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    If new door does NOT equal the door with the prize in it, then you lose. So you need to compare newdoor with the winning door (which may or may not be zero).

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