Dice Rolling Problem

This is a discussion on Dice Rolling Problem within the C++ Programming forums, part of the General Programming Boards category; Roll 2 dice 36000 times and tally the results using an array. I'm very confused. My program: Code: #include <iostream> ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    13

    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!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.
    Last edited by robatino; 05-26-2007 at 01:15 PM.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    88
    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";
    }
    Last edited by UMR_Student; 05-26-2007 at 01:15 PM.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    13
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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