Dice Rolling Problem

• 05-26-2007
EdSquareCat
Dice Rolling Problem
Roll 2 dice 36000 times and tally the results using an array.

I'm very confused.

My program:

Code:

```#include <iostream> using std::cout; using std::cin; #include <cstdlib> #include <ctime> int roll_die( int ); void display( int [] ); int main() {     int roll1;     int roll2;     int total;     int num[ 11 ];     srand( time( 0 ) );     for ( int a = 0; a < 36000; a++ ) {         roll1 = roll_die( roll1 );         roll2 = roll_die( roll2 );         total = roll1 + roll2;         num[ total - 1 ] = 1;     }     display( num );     cin.get();     return 0; } int roll_die( int roll ) {     roll = 1 + rand() % 6;     return roll; } void display( int number [] ) {     for ( int b = 0; b < 12; b++ )         cout << b + 1 << " was rolled " << number[ b ] << " times.\n"; }```
My output:
Code:

```1 was rolled 2009252814 times. 2 was rolled 1 times. 3 was rolled 1 times. 4 was rolled 1 times. 5 was rolled 1 times. 6 was rolled 1 times. 7 was rolled 1 times. 8 was rolled 1 times. 9 was rolled 1 times. 10 was rolled 1 times. 11 was rolled 1 times. 12 was rolled 1 times.```
I understand that I might be doing this completely wrong; I haven't programmed in months, and I wasn't very good at it in the first place!
• 05-26-2007
robatino
Code:

`  num[ total - 1 ] = 1;`
You never change the elements of num[] anywhere else. The value of num[0] is an uninitialized value, since you don't initialize the elements of num[] to zero and total is never 1. Also, roll_die() should be declared as
Code:

`int roll_die()`
and the variable roll should be a local variable in the function body, since you never use the value you're presently passing.

Edit: Just to be clear, what you need to do is INCREMENT num[total-1], not set it to 1:
Code:

`  ++num[total-1];`
but don't forget to initialize all the elements of num[] to 0 first.

Edit: It should also be
Code:

`  int num[12];`
Edit: The guaranteed maximum value of a signed short or int is only 32767, but num[] should be a type with a guaranteed maximum of at least 36000. An unsigned short or int has a guaranteed maximum of 65535. Of course, it's very unlikely (read "lifetime-of-the-universe unlikely") that you would get a single value more than 32767 times with 36000 rolls, but still.
• 05-26-2007
UMR_Student
Code:

```#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int roll_die(); void display( int [] ); //This is kind of stupid, but whatever const int MAX_SUM = 6 + 6; const int NUM_ROLLS = 36000; int main() {     int num[MAX_SUM];     srand( time( 0 ) );     for (int i = 0; i < MAX_SUM; i++)             num[i] = 0;     for ( int a = 0; a < 36000; a++ )             num[roll_die() + roll_die() - 1]++;         display( num );   } int roll_die( ) {     int roll = (rand() &#37; 6) + 1;     return roll; } void display( int number [] ) {     //Notice I changed b's initial value to 1     for ( int b = 1; b < 12; b++ )         cout << b + 1 << " was rolled " << number[ b ] << " times.\n"; }```
• 05-26-2007
EdSquareCat
Thanks, robatino, the problem was that I didn't initialize. Agh that was supposed to say += not = !!! Haha thats why I got all those 1's :)