Jump to content

Point me in the right direction

- - - - -

  • Please log in to reply
32 replies to this topic

#1
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
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!

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Actually I was more thinking something like:
bool doors[3] = {false, false, false};
doors[ rand() % 3 ] = true;

/* do whatever */

sudo rm -rf /

#7
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Ok, so let me see if I understand

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"?

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Correct.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
The one set to true should be your correct door.
sudo rm -rf /

#12
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
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