Help with a program...

This is a discussion on Help with a program... within the C Programming forums, part of the General Programming Boards category; Hi! I've been interested in programming for a while now, so a couple of months ago a bought a book ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    Help with a program...

    Hi! I've been interested in programming for a while now, so a couple of months ago a bought a book so I could teach myself. The thing is, I'm having some trouble with an excercise. It's objective is to build a program that, using arrays, shows the results of throwing two dice 36.000 times. I have written up this code:

    Code:
    // Ej. 6.19
    // two dice throws
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define N 13
    
    // main starts program
    int main()
    {
        int total; // total of two dice throws
        int throws; // counter for number of throws
        int frec[N]; // frequency of dice throws
        int start; // user starts program with this
        
        srand( time (NULL) ); // random number generator
        
        printf( "\nProgram to simulate the throwing of 2 dice 36000 times\n" );
        
        
        do
        {
            printf( "To start, type 1.\n" );
            printf( "Otherwise, type any other number to exit.\n" );
            scanf( "%d", &start );
            
            if( start != 1 )
                break;
            
            // throws dice 36000 times
            for( throws = 1; throws <= 36000; throws++ )
            {
                total = ( 1 + ( rand() % 6 ) ) + ( 1 + ( rand() % 6 ) );
                frec[ total ]++;
            } // end of for
            
            printf( "%s%17s\n", "Total", "Frequency" );
            
            for( total = 1; total < N; total++ )
            {
                printf( "%5d%17d\n", total, frec[total] );
            } // end of for
        } // end of do
        while( start  == 1 ); 
        
        return 0; // successfull end of program
    } // end of main
    But, the actual program running is really strane. The compiler (Dev-C++) didn't give any errors or warnings or anything.

    Code:
    Program to simulate the throwing of 2 dice 36000 times
    To start, type 1.
    Otherwise, type any other number to exit.
    1
    Total        Frequency
        1       2009284272
        2          2294727
        3       2009286276
        4       2009148402
        5          2297449
        6       2009251663
        7          2299872
        8       2009289304
        9       2009149352
       10             2980
       11       2009248549
       12       2009306949
    To start, type 1.
    Otherwise, type any other number to exit.
    Huge numbers.... No matter how many times I run it, even with srand, the numbers are about the same...
    Could anyone tell me what's wrong with my code, please?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Make sure you initialize all your array elements to zero.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Now it works perfectly! Thanks! I've always overlooked that... I've just started learning to program, anyway.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Also you will never have a total of one because the lowest roll for each die is a one, and one plus one is two.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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