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?
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); }
Again I have basic knowledge of programming, so this is very difficult for me and I know that this code is incorrect.
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, 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.
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); }
I am pretty sure there is still a problem with the random number though
Several of your if statements are using the assignment (Single = sign) operator as opposed to the equality comparison operator (double == sign).
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); }