# Thread: Little help finding a bug in this program

1. ## 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. 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.

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. 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

5. Ummm....the chance is 1 in 11. Exactly.

Whatever else you are doing is pointless.

6. Originally Posted by MK27
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

7. Code:
`      if (sum == 8)`

8. Originally Posted by abachler
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

9. Originally Posted by abachler
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. Originally Posted by MK27
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.

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

11. 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;
}```

12. Good catch, Abachler.