C Program to The Monty Hall Problem

How to write a C Program to The Monty Hall Problem ?


Solution:

#include<stdio.h>

typedef char * string;

int main(void)
{

    int st, sw, i, x;
    char *p, *a, *b, *s;
    st = 0; //count the number of wins with staying
    sw = 0; //count the number of wins with switching

    string door[3];//create 3 doors (4 really)

    srand(time(NULL));//required for random numbers
    printf("Stay Wins -- Switch Wins\n");

    //loop 100 times
    for(i=0;i<100;i++){
        //assign 3 door values
        door[1] = "win";
        door[2] = "lose";
        door[3] = "lose";

        x=rand() % 3 + 1; //pick a random door
        p = door[x]; //remember player's choice
        if(x==1){
            s="lose"; //if player picked door 1 switching loses
            st++;  //log stay win
        }
        else{
            s="win"; //if player picked door 1 switching wins
            sw++;  //log switch win
        }
        printf("   %s   --   %s   \n", p, s);
    }
    printf("Staying will win %i of the time.\n", st);
    printf("Switching will win %i of the time.\n", sw);
    return 0;
}

C Program Quadratic equation


Learn More :