Hi, I understand you guys do not give answers on homework or anything, but don't worry I am not wanting the answer I want to solve it on my own :). I have to write a simple lets make a deal program.
[Details
The game will be played where the contestant selects a door out of 3, the host then opens a door that is incorrect. The contestant then chooses to either keep his original choice or switch it to the other door. If he gets it right he win if not he loses. I need to write it so it runs it automatically. The other catch is I need to do it where it runs 1000 times where the contestant does not switch his choice, 1000 times when he does switch his choice and then 1000 times where its random.
So I was needing to know where I can find some tutorials or what type of commands I need to read up on. My book for this class is terrible and very hard to understand. Thanks for the help!
32 replies to this topic
#1
Posted 18 October 2010 - 04:22 PM
|
|
|
#2
Posted 18 October 2010 - 04:37 PM
Well, for the random part you're going to want to use either stdlib.h or cstdlib depending on whether you're using C or C++, respectively. Then:
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
... blah...
/* seed random number generator */
srand(time(NULL));
... blah...
/* get random door */
correct_door = rand() % 3;
initial_chosen_door = rand() % 3;
...
}
You can hard-code two loops to have the user pick the same door, the other door, and then on your third loop you can use rand() again to switch the choice.
sudo rm -rf /
#3
Posted 18 October 2010 - 04:44 PM
Ok I am a little stumped. I understand how to generate the random number. But should I just set it assign the numbers 1,2, and 3 to a random door.
Edited by dargueta, 19 October 2010 - 07:44 PM.
Double post
#4
Posted 19 October 2010 - 07:45 PM
0-2, because you're probably going to use an array. If not then 1-3. I'm not entirely sure what your question is, though.
sudo rm -rf /
#5
Posted 19 October 2010 - 08:40 PM
Yeah, I am just really confused about the whole thing. I assigned number 0 to be the winning number, and numbers 2 and 3 to be the losing numbers. I get how to make it select a random number, I am just very unsure how to then assign that number to a door. I mean do I need to label each door as
door_1 = rand ( ) % 3;
if (door_1 == 0) {
door_1 = winning_door
door_2 != 0
door_3 != 0
} /* Repeat for each door and possible number for each door*/
Edited by JanitorMoe, 19 October 2010 - 09:55 PM.
#6
Posted 19 October 2010 - 10:30 PM
Actually I was more thinking something like:
bool doors[3] = {false, false, false};
doors[ rand() % 3 ] = true;
/* do whatever */
sudo rm -rf /
#7
Posted 19 October 2010 - 11:31 PM
I haven't learned about bool yet, but if its more effective I would like to read up on it. I cannot find anything on the web that is clear about it in C. Do you know if a link or page that will explain what it is and how to use it?
#8
Posted 19 October 2010 - 11:36 PM
A bool is usually implemented as an int, though that varies from compiler to compiler and machine to machine. It's a relatively simple concept:
int x = 5, y = 6;
bool comp;
if( x != y )
do_something();
comp = (x != y);
if( comp )
do_something();
do_something() should be executed twice. Anything in a conditional expression--for, while, if blocks--is for all intents and purposes a boolean expression, either true or false.
sudo rm -rf /
#9
Posted 20 October 2010 - 12:11 AM
Ok, so let me see if I understand
The above code means there are 3 doors all of which are "false" and randomly one of them will be chosen to be "true"?
bool doors[3] = {false, false, false};
doors[ rand() % 3 ] = true;
/* do whatever */
The above code means there are 3 doors all of which are "false" and randomly one of them will be chosen to be "true"?
#11
Posted 20 October 2010 - 12:54 AM
#12
Posted 20 October 2010 - 06:26 AM
So then in turn I could use this?
if (door [1] == true) {
door [1] = winning_door
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









