Thread: Check for efficiency and errors in this program

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    11

    Check for efficiency and errors in this program

    Hi, I came with yet another program. I've done all the requirements my professor asked of me which is to write a Monty Hall program with research mode and game mode and also to have modules. I've did all that, but I'm not sure whether I can further improve this program (add more modules, get rid of stuff, etc.) or not.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int WINNING (int win);
    float PERCENTAGE (float win,float playedgames);
    int RANDOM ();
    
    
    int main (void)
    {
        int choice, choice2, Ngames, Ngames2,chosendoor,chosendoor2;
        int gamesplayed,gamesplayed2,winningdoor,winningdoor2,revealeddoor;
        int switcheddoor, revealeddoor2,stayeddoor, doorchoice,winningdoor3;
        int revealeddoor3, choice3, switcheddoor2;
        float percent,percent2;
        float win = 0,win2 = 0;
        int gamesplayed3 = 0,win3 = 0;
    
    
        srand(time(NULL)); //randomizes based on time
    
    
        while (choice!=3)
        {
            printf ("This programs lets you either play or research the Monty Hall game.\n");
            printf ("Please choose an option.\n(enter the integer near the option)\n1. Research mode\n2. Game mode\n3. Exit\n\n");
            scanf ("%d", &choice);
    
    
            switch(choice)
            {
            case 1:
                printf ("\nYou have chosen research mode.\n");
                printf ("Please indicate whether you will always switch doors or never switch doors.\n");
                printf ("1. Always switch doors\n2. Never switch doors\n\n");
                scanf ("%d", &choice2);
    
    
                switch (choice2)
                {
                case 1:
                    printf ("\nYou have chosen to always switch doors.\n");
                    printf ("How many games would you like to play?\n\n");
                    scanf ("%d", &Ngames);
    
    
                    for (gamesplayed = 0;gamesplayed <= Ngames;gamesplayed++)
                    {
                        winningdoor = RANDOM();
                        chosendoor = 1;
    
    
                        do
                        {
                            revealeddoor = RANDOM();
                        } while (revealeddoor == chosendoor || revealeddoor == winningdoor);
    
    
                        do
                        {
                            switcheddoor = RANDOM();
                        } while (switcheddoor == chosendoor || switcheddoor == revealeddoor);
    
    
                        if (switcheddoor == winningdoor)
                        {
                            win = WINNING(win);
                        }
                    }
    
    
                    percent = PERCENTAGE(win,gamesplayed);
    
    
                    printf ("\nYou have chosen the winning door %f percent of the time.\n\n", percent);
    
    
                    win = 0;
                    break;
    
    
                case 2:
                    printf ("\nYou have chosen to never switch doors.\n");
                    printf ("How many games would you like to play?\n\n");
                    scanf ("%d", &Ngames2);
    
    
                    for (gamesplayed2 = 0;gamesplayed2 <= Ngames2; gamesplayed2++)
                    {
                        winningdoor2 = RANDOM();
                        chosendoor2 = 1;
    
    
                        do
                        {
                            revealeddoor2 = RANDOM();
                        } while (revealeddoor2 == winningdoor2 || revealeddoor2 == chosendoor2);
    
    
                        do
                        {
                            stayeddoor = RANDOM();
                        } while (stayeddoor != chosendoor2);
    
    
                        if (stayeddoor == winningdoor2)
                        {
                            win2 = WINNING(win2);
                        }
                    }
    
    
                    percent2 = PERCENTAGE(win2,gamesplayed2);
    
    
                    printf ("\nYou chose the winning door %f percent of the time.\n\n", percent2);
    
    
                    win2 = 0;
                    break;
    
    
                default:
                    printf ("You have not entered a correct value.");
                    break;
                }
                break;
    
    
            case 2:
                printf ("\nYou have chosen game mode.\n");
    
    
                winningdoor3 = RANDOM();
    
    
                printf ("Which door will you choose? (Door 1,2, or 3)\n\n");
                scanf ("%d", &doorchoice);
    
    
                if (doorchoice < 1 || doorchoice > 3)
                {
                    printf ("That value is not a door.\n\n");
                    break;
                }
    
    
                do
                {
                    revealeddoor3 = RANDOM();
                } while  (revealeddoor3 == doorchoice);
    
    
                printf ("\nDoor %d was opened and there was no prize behind it.\n", revealeddoor3);
                printf ("Would you like to:\n1. Switch doors?\n2. Stay with your current door?\n\n");
                scanf ("%d", &choice3);
    
    
                switch (choice3)
                {
                case 1:
                    printf ("\nYou have chosen to switch doors.\n\n");
    
    
                    do
                    {
                        switcheddoor2 = rand()%2 + 1;
                    } while (switcheddoor2 == doorchoice);
    
    
                    if (switcheddoor2 == winningdoor3)
                    {
                        printf ("Good choice! You have won!\n\n");
                        win3 = WINNING(win3);
                    }
    
    
                    else
                    {
                        printf ("Unfortunately, you lost.\n\n");
                    }
    
    
                    gamesplayed3 = gamesplayed3 + 1;
    
    
                    printf ("You have played a total of %d games and you won a total of %d games.\n", gamesplayed3, win3);
                    break;
    
    
                case 2:
                    printf ("\nYou have decided to stay with your current door.\n");
    
    
                    if (doorchoice == winningdoor3)
                    {
                        printf ("Good choice! You have won!\n\n");
                        win3 = WINNING(win3);
                    }
    
    
                    else
                    {
                        printf ("Unfortunately, you lost.\n\n");
                    }
    
    
                    gamesplayed3 = gamesplayed3 + 1;
    
    
                    printf ("You have played a total of %d games and you won a total of %d games.\n\n", gamesplayed3, win3);
                    break;
    
    
                default:
                    printf ("You have not entered a correct value.\n\n");
                    break;
                }
                break;
    
    
                case 3:
                    exit(0);
    
    
                default:
                    printf ("You have not entered a correct value.\n\n");
                    break;
            }
        }
        return 0;
    }
    
    
    int WINNING (int win)
    {
        int newwin;
        newwin = win + 1;
        return (newwin);
    }
    
    
    float PERCENTAGE (float win,float playedgames)
    {
        float percentage;
        percentage = (win/playedgames)*100;
        return (percentage);
    }
    
    
    int RANDOM ()
    {
        int random;
        random = rand()%3;
        return (random);
    }
    I'm also worried that when I play game mode, I lose a bit too much when I switch doors, but I see no problem in the code.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks like it has an off by one error:
    Code:
    for (gamesplayed = 0;gamesplayed <= Ngames;gamesplayed++)
    Typically, we would iterate from index = 0 to index < N, or we would iterate from index = 1 to index <= N. In this case, you are iterating from index = 0 to index <= N, which means that you probably have an extra iteration. The same thing happens in a later for loop.

    It is good that you defined helper functions, but I suggest that you adopt a different naming convention for those function names. It is a common convention to use names that are in uppercase for macros and constants.

    I suggest that you move code from your big switch statement into functions. The "play mode" and "research mode" options probably should have their own functions. Otherwise, it is rather difficult to read your main function because of the length and nesting of switch statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    main() exhibits undefined behaviour since choice is uninitialised, and the first thing done is enter a loop testing the value of choice.

    There is a number of instances of code which looks like
    Code:
    do
    {
              some_variable = some_random_generator();
    } while (some_variable is in some set of values);
    Try working out some way to simplify that, say with the help of a function or two.

    Statements like "gamesplayed3 = gamesplayed3 + 1;" are arguably more readable as "++gamesplayed3;"

    There is a number of instances of code that does something like
    Code:
    printf(some_string);
    printf(some_other_string);
    scanf("%d", some_int);
    See if you can use a function to reduce the repetition and simplify.

    Your code is never checking return values from "scanf("%d", &some_int)". To complicate things, those scanf() statements are in (nested) loops. Any user can therefore cause your program to loop forever by simply entering a letter (or any character that is not whitespace or a digit).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check for efficiency
    By rgrwatson85 in forum C Programming
    Replies: 8
    Last Post: 02-28-2011, 09:27 PM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Anybody know of a program to test efficiency?
    By John_ in forum Tech Board
    Replies: 2
    Last Post: 01-26-2006, 02:13 AM
  4. WindChill Program.Efficiency
    By AdamanC in forum C Programming
    Replies: 15
    Last Post: 08-27-2004, 08:26 AM
  5. Replies: 1
    Last Post: 04-30-2003, 11:17 AM

Tags for this Thread