Little help finding a bug in this program

This is a discussion on Little help finding a bug in this program within the C Programming forums, part of the General Programming Boards category; Here's the question: This program is intended to calculate the probability that if a person rolls two fair sixed-sided dice, ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    32

    Little help finding a bug in this program

    Here's the question:

    This program is intended to calculate the probability that if a person
    rolls two fair sixed-sided dice, that they will get a total of 8. This
    is done by looping through all the possibilities and counting those that
    do not have a sum that exceeds 9. However, there are some bugs in this
    file. Can you find them?
    Code:
    #include <stdio.h>
    
    int main() {
    
      int count, total;
    
      for (i=1; i<6; i++)
        for (j=0; j<=6; j++)
          sum = i+j;
          if (sum = 8)
            count++;
          total++;
    
      printf("The probability of rolling an 8 is %lf.\n", count/total);
    
      return 0;
    }
    So obviosuly, there are some undeclared variables so this is what I wrote:


    Code:
    int main() {
    
      int count, total;
      int i;
      int j;
      double sum=0;
      
      for (i=1; i<6; i++) {
        for (j=0; j<=6; j++)
          sum = i+j;
          if (sum = 8)
            count++;
          total++;
          }
    
      printf("The probability of rolling an 8 is %lf.\n", count/total);
      
      system("PAUSE");
      return 0;
    }
    From there, I'm not too clear of how to make it work

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The loop counters i and j both need to go from 1 to 6. No 0's, and no < 6. < 7 is OK though (preferred IMHO).

    After sum you need two = signs. sum == 8. Also, you need to set total and count variables = 0, before the for loops.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    Quote Originally Posted by Adak View Post
    The loop counters i and j both need to go from 1 to 6. No 0's, and no < 6. < 7 is OK though (preferred IMHO).

    After sum you need two = signs. sum == 8. Also, you need to set total and count variables = 0, before the for loops.
    For the first comment, did you mean like this:

    Code:
    int main() {
    
      int count, total;
      int i=0;
      int j=0;
      double sum=0;
      
      for (i=1; i<6; i++) {
        for (j=1; j<6; j++)
          sum = i+j;
          if (sum == 8){
            count++;
            }
          total++;
          }
    
      printf("The probability of rolling an 8 is %lf.\n", count/total);
      system("PAUSE");
      return 0;
    }

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    again, i<6 is incorrect, that will get you 1-5 only, you need i<7 in both for loops

    other than that it looks good
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ummm....the chance is 1 in 11. Exactly.

    Whatever else you are doing is pointless.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by MK27 View Post
    Ummm....the chance is 1 in 11. Exactly.

    Whatever else you are doing is pointless.
    you are wrong mk27, so horribly wrong.

    The True Odds of rolling two dice
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
          if (sum == 8)
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by abachler View Post
    you are wrong mk27, so horribly wrong.

    The True Odds of rolling two dice
    Okay I see the point now. Sorry.

    I always just bet on the red squares
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    Quote Originally Posted by abachler View Post
    again, i<6 is incorrect, that will get you 1-5 only, you need i<7 in both for loops

    other than that it looks good
    Something else is wrong. The output says the prob. is zero.

    I'm pretty sure it has something to do with increments

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by MK27 View Post
    Ummm....the chance is 1 in 11. Exactly.

    Whatever else you are doing is pointless.

    You couldn't be more wrong, MK27. He's learning how to code up a C program that finds the solution.

    Since the answer is already known, he can check his program's answer.

    The variables i and j need to go from 1 to < 7, not 1 to < 6

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    nop, he forgot ot enclose his inner loop in curly braces

    Code:
    int main() {
    
      int count, total;
      int i=0;
      int j=0;
      double sum=0;
      
      for (i=1; i<6; i++) {
        for (j=1; j<6; j++){
          if (i+j == 8) count++;
          total++;
          }
        }
    
      printf("The probability of rolling an 8 is %lf.\n", count/total);
      system("PAUSE");
      return 0;
    }
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good catch, Abachler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slot Machine program c++ bug
    By nobo707 in forum C++ Programming
    Replies: 12
    Last Post: 09-23-2009, 07:29 PM
  2. Disappearing Bug
    By el-sid in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2009, 12:12 PM
  3. Kernel bug? (attn matsp)
    By brewbuck in forum Linux Programming
    Replies: 7
    Last Post: 04-13-2009, 10:31 AM
  4. Bug in Program - please help!
    By muffin in forum C Programming
    Replies: 4
    Last Post: 08-31-2001, 09:33 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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