Logical errors with if and if-else statements

This is a discussion on Logical errors with if and if-else statements within the C Programming forums, part of the General Programming Boards category; I am trying to recreate the Monty Hall problem, statistically. For those of you that are not familiar with the ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    20

    Logical errors with if and if-else statements

    I am trying to recreate the Monty Hall problem, statistically. For those of you that are not familiar with the Monty Hall problem, I will skim through the idea behind it. You are on a game show and you get to pick between 3 doors. One door has a car, and the two other doors have goats. You get to pick one door. Once you pick a door, the game show host will open one of the other doors, which will reveal where one of the goats is hidden. The game show host then asks if you would like to switch to another door. Now the idea is that, statistically, you have a higher chance of winning the car if you switch doors. This explains the basic concept behind the problem. I won't explain the problem in any more detail because this sums up the basic concept of it. It's a cool problem, so if you are not familiar with it, you should check it out.

    Anyway, I've completed the program, but I can't figure out why I keep getting different results for the switch_wins and switch_losses scenarios. Also, I get less results for switch_wins_switch losses than what should be expected. Maybe one of my if or if else statements is faulted. I can't for the life of me figure this out though.

    Edit:

    A few things I neglected to mention previously, the door number always starts on door 1. The car is stored in my arrays as 1 and a goat is stored as 2. You can see them being stored inside of my arrays in the beginning.

    The following code is where I think the problem is occurring:

    Code:
      if(x%2 == 0){
        even += 1;
        if(b[x] != 1){
          if(c[x] == 1){
            switch_wins += 1.0;
          }
          else if(a[x] == 1){
            switch_losses += 1.0;
          }
        }
        else if(c[x] != 1){
          if(b[x] == 1){
            switch_wins +=  1.0;
          }
          else if(a[x] == 1){
            switch_losses += 1.0;
          }
        }
      }
    Here is the entire program:

    Code:
    /*This program recreates the Monty Hall problem, statistically*/
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    main()
    {
    int x;
    int count=0;
    float switch_win_percentage = 0.0;
    float dont_switch_win_percentage = 0.0;
    int total_wins_losses;
    float switch_wins=0.0;
    float dont_switch_wins=0.0;
    int a[100],b[100],c[100];
    float switch_losses = 0.0;
    float dont_switch_losses = 0.0;
    int odd=0;
    int even =0;
    
      printf("Choose the number of trials.\n");
      printf("Must be an even integer\n-> ");
      do{
        scanf("%d",&count);
        getchar();
      }while(count % 2 == 1 || count%2 == 1);
    
      srand(time(NULL));
      for(x=0;x<count;x++){
        a[x] = 1+rand()%2;
        b[x] = 1+rand()%2;
        c[x] = 1+rand()%2; 
    	if(a[x] == 1 && b[x] == 1 ){
    	  x--;
    	  continue;
    	}
    	else if ( b[x] == 1 &&  c[x] == 1 ){
              x--;
    	  continue;
    	}
    	else if ( a[x] == 1 && c[x] == 1 ){
              x--;
    	  continue;
    	}
    	else if (a[x] == 2 && b[x] == 2 && c[x] == 2){
              x--;
    	  continue;
    	}
      }
      count *= 2;
      for(x=0;x<count;x++){
        if(x%2 == 0){
          even += 1;
          if(b[x] != 1){
            if(c[x] == 1){
    	  switch_wins += 1.0;
    	}
    	else if(a[x] == 1){
              switch_losses += 1.0;
           	}
          }
          else if(c[x] != 1){
            if(b[x] == 1){
              switch_wins +=  1.0;
    	}
    	else if(a[x] == 1){
    	  switch_losses += 1.0;
    	}
          }
        }
        else if(x%2 == 1 || x == 1){
          odd += 1;
          if (a[x] == 1){
            dont_switch_wins += 1.0;
          }
          else{
            dont_switch_losses += 1.0;
          }
        }
      } 
      printf("Odd: %d\n",odd);
      printf("even: %d\n\n",even);
      getch();
     
      printf("switch_wins: %f\n",switch_wins);
      printf("switch_loss: %f\n",switch_losses);
      total_wins_losses = switch_wins + switch_losses;
      printf("Total switch wins and losses: %d\n\n",total_wins_losses);
      
      printf("dont_switch_wins: %f\n",dont_switch_wins);
      printf("dont_switch_lossses: %f\n",dont_switch_losses);
      total_wins_losses = dont_switch_wins + dont_switch_losses;
      printf("Total don't switch wins and losses: %d\n\n",total_wins_losses);
      
      total_wins_losses = switch_wins+switch_losses+dont_switch_wins+dont_switch_losses;
      printf("Total of all: %d\n",total_wins_losses);
      printf("Total trials: %d\n\n",count);
      getch();
      count /= 2;
      switch_win_percentage = (switch_wins * 100)/count;
      dont_switch_win_percentage = (dont_switch_wins * 100)/count;
      printf("Switch win Percentage: %f%% \n",switch_win_percentage);
      printf("Don't switch win Percentage: %f%% \n",dont_switch_win_percentage);
      getch();
      
      printf("\n\nExiting program\n");
      getch();
      return 0;
    } /****end of main function****/
    I went from having all of this partitioned into different functions, to this. I've added the odd and even variables to make sure that my logic pertaining to my odd and even number check wasn't faulted, which it doesn't seem to be. Some of my printf statements are for debugging, so they may come in handy to who ever checks this out.

    Including yesterday, I've been working on this for 7+ hours. Help would be greatly appreciated.

    ~Billy
    Last edited by billybob2020; 11-18-2010 at 01:15 PM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    It actually has nothing to do with the chain of logic.
    Code:
    for(x=0;x<count;x++){
        a[x] = 1+rand()%2;
        b[x] = 1+rand()%2;
        c[x] = 1+rand()%2; 
        ...
    }
    count *= 2;
    
    for(x=0;x<count;x++){
        ...
        if(b[x] != 1){
            if(c[x] == 1){
        ...
    }
    See what's going on here?
    You're initializing the array with count items, but accessing it later in the range of 0 to count*2.
    It's just that C doesn't do bounds checking - you were using a bunch of garbage data.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    20
    Awesome reply bernt. I will have fun playing with this program for a bit now.

    Thanks a bunch!

Popular pages Recent additions subscribe to a feed

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