Need Help with "Monty Hall" simulator program.

This is a discussion on Need Help with "Monty Hall" simulator program. within the C++ Programming forums, part of the General Programming Boards category; Hello All, I am currently a beginner C++ programmer. Here I have a program that simulates the "Monty Hall" problem. ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    8

    Need Help with "Monty Hall" simulator program.

    Hello All,

    I am currently a beginner C++ programmer.

    Here I have a program that simulates the "Monty Hall" problem. At first, the program prompted the user to choose what door they wanted to pick, and then went from there.

    I restructured it so that the computer simulates everything by itself and runs 10,000 times. However, as you will see in the code, the last part of my main function has variables that are not defined outside of the for loop.

    So, my question is, how do I forward those 4 variables out of the for loop so that I can use them to output the final results? I feel like this is a simple problem, but for whatever reason I cannot wrap my head around the answer.

    Ideally, the final result will look something like:
    "After running the program 10,000 times, the final results were:

    x wins out of x stays.

    x wins out of x switches."

    Thank you for any help provided. I will answer any necessary questions as best as I can. I apologize in advance for any code that is messy, inefficient, or otherwise unsatisfactory. As I stated I am a beginning programmer who is still trying to wrap his head around functions.



    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;
    
    int getUserChoice ();
    
    int determinePrizeLocation ();
    
    int getFinalChoice (int firstChoice, int grandPrize);
    
    bool getSwitchOrStay (int firstChoice, int finalChoice);
    
    void determineOutcomeStay (int finalChoice, int grandPrize, int winStay, int numOfStay);
    
    void determineOutcomeSwitch (int finalChoice, int grandPrize, int winStay, int numOfSwitch);
    
    void outputFinalResult (int winStay, int winSwitch, int numOfStay, int numOfSwitch);
    
    int main ()
    {
    	for (int i = 0; i > 10000; i++) 
    	{
    		srand (time(0));
    
    		int firstChoice;
    
    		int grandPrize;
    
    		int finalChoice;
    
    		int winStay = 0;
    
    		int winSwitch = 0;
    
    		int numOfStay = 0;
    
    		int numOfSwitch = 0;
    
    		firstChoice = getUserChoice ();
    
    		grandPrize = determinePrizeLocation ();
    
    		finalChoice = getFinalChoice (firstChoice, grandPrize);
    
    		bool switchOrStay = getSwitchOrStay (firstChoice, finalChoice);
    
    		if (switchOrStay == true)
    			determineOutcomeStay (finalChoice, grandPrize, winStay, numOfStay);
    
    		else
    			determineOutcomeSwitch (finalChoice, grandPrize, winSwitch, numOfSwitch);
    	}
    
    	outputFinalResult (winStay, winSwitch, numOfStay, numOfSwitch);
    
    	return 0;
    }
    
    int getUserChoice ()
    {
    	return rand () % 3 + 1; // Random first choice
    }
    
    int determinePrizeLocation ()
    {
    	int grandPrize = rand () % 3 + 1; // Grand prize is placed randomly
    
    	return grandPrize;
    }
    
    int getFinalChoice (int firstChoice, int grandPrize)
    {
    	int revealedDoor = rand () % 3 + 1; // Random number btw 1-3
    
    	int finalChoice;
    
    	while (revealedDoor == grandPrize || revealedDoor == firstChoice) // Reveal whichever door isn't the grand prize or the door user selected
    	{
    		revealedDoor = rand () % 3 + 1;
    	}
    	
    	int switchProbability = rand () % 2 + 1; // 50% chance of keeping or switching choice
    
    	if (switchProbability == 1) // Represents user keeping their choice
    	{
    		return firstChoice;
    	}
    
    	else if (switchProbability == 2) // Represents user switching their choice
    	{
    		finalChoice = rand () % 3 + 1; // Randomizes finalChoice btw 1-3
    
    		while (finalChoice == revealedDoor || finalChoice == firstChoice) // Ensures that finalChoice isn't the door that was eliminated or
    		{                                                                 // the door that was initially selected
    			finalChoice = rand () % 3 + 1;
    		}
    	
    		return finalChoice;
    	}
    }
    
    bool getSwitchOrStay (int firstChoice, int finalChoice)
    {
    	if (finalChoice == firstChoice)
    		return true;
    
    	else
    		return false;
    }
    
    void determineOutcomeStay (int choice, int grandPrize, int winStay, int numOfStay)
    {
    	if (choice == grandPrize)
    	{
    		winStay++; // Increments winStay by 1 if user won by staying with their first choice	
    	}
    
    	numOfStay++;
    }
    
    void determineOutcomeSwitch (int choice, int grandPrize, int winSwitch, int numOfSwitch)
    {
    	if (choice == grandPrize)
    	{
    		winSwitch++; // Increments winSwitch by 1 if user won by switching their first choice
    	}
    
    	numOfSwitch++;
    }
    
    void outputFinalResult (int winStay, int winSwitch, int numOfStay, int numOfSwitch)
    {
    	cout << "\nAfter running the program 10,000 times, the final results were:" << endl;
    	cout << "\n" << winStay << " wins out of " << numOfStay << " stays." << endl;
    	cout << "\n" << winSwitch << " wins out of " << numOfSwitch << " switches." << endl;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    Variables which accumulate their value through multiple iterations of a loop, must be declared outside that loop, in your case at the top of main.

    Also, in order for a function to change the value of one of the parameters passed to it, that parameter must be passed by reference. This is most easily achieved by changing it's type to a reference. For instance change "int" to "int&". References have other uses too, so it would behoove you to learn about them through a textbook.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > for (int i = 0; i > 10000; i++)
    Perhaps you meant < ?

    > srand (time(0));
    Move this outside your for loop.
    You should only do this ONCE in any single run of your program.
    In a short program, time(0) effectively returns the same value, so all your rand() values will be the same as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MONTY HALL problem
    By jackalope in forum C Programming
    Replies: 10
    Last Post: 10-28-2010, 09:43 PM
  2. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21