I've got this exercise from a book that wants me to program two rolling dice, add the value of the dice to together and basically track each time a number is rolled. So ive got an array with 11 spots for the numbers 2 - 12. Whats not working in my code is when i want it to add another roll to 9. The dice are rolled 36000 times, so no value should be above 36000. But they are either around 1226503 or sometimes -1075346436.

int a[10];

if (sum == 2) {
    a[0] += 1;
}

if (sum == 3) {
    a[1] += 1;
}

//This continues all the way to a[10], sum == 12

i've printed each sum that the dice are rolling, and that part of the code is fine so i didnt post it.

Hi

>>> //This continues all the way to a[10], sum == 12

1. why not replacing them all by a single: a[sum]++; ?
2. did you really start with initializing a: for(i=0;i<12;i++)a=0; ?
3. using old compiler where int is 16 bit ?

-- tesu

well at first i used:

for (roll = 0; roll < 36000; roll++) {
    sum = rollDice();
    a[sum - 2] +=1

But after having the huge numbers i thought maybe splitting it to separate if statements might help, it didnt. My for statement is still there.

As for question 3. Im using gcc on ubuntu 10, so not sure if its 16 bit or not. The book im following is from 2004.

My print statement to check the values is as follows:

printf("%d %d %d %d %d %d %d %d %d %d %d", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10]);

Hi

>>> //This continues all the way to a[10], sum == 12

1. why not replacing them all by a single: a[sum]++; ?
2. did you really start with initializing a: for(i=0;i<12;i++)a=0; ?
3. using old compiler where int is 16 bit ?

-- tesu

a[sum]++; and a[sum - 2]++;

produced same result as before

in which case, here's the whole code:

#include <stdio.h>

int rollDice(void);

int main()
{
    int roll;
    int a[10];
    int sum;
    for (roll = 0; roll < 36000; roll++) {
        sum = rollDice(); 
        a[sum - 2] +=1;
    }

    printf("%d %d %d %d %d %d %d %d %d %d %d", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10]);

}

int rollDice(void)
{
    int die1;
    int die2;
    int diesum;
 
    die1 = 1 +( rand() % 6)
    die2 = 1 +( rand() % 6)
    diesum = die1 + die2;

    return diesum;

}

I got some help from a rep of the publisher. And he noted i needed to initialize each value of the array of 0 first. so here's what ive done:

int main()
{    
   int i;    
   int a[10];    
   int sum;    

   for (i = 0; i <= 11; i++) {
       a[i] = 0
   }

   for (i = 0; i < 36000; i++) {       
       sum = rollDice();        
        a[sum - 2] +=1;    
}

after compiling and running it, it just seems to keep running. Not sure how its gotten stuck.

K its stuck because:

for (i = 0; i < 11; 1++)
\\was supposed to be:
for (i = 0; i < 10; 1++)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.