Thread: Frequency Array

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    19

    Frequency Array

    Good Afternoon,

    I am working on a problem from the Dietel & Dietel C Programming Book. I have solved the exercise (I think), but I am getting erroneous results (my error). Could you guys take a look at this and help me out?

    Here is the problem:

    //Dietel & Dietel C Programming
    //Chapter 6 Arrays: Page 241 Exercise: 6.19
    /* Write a program that simulates the rolling of two dice.

    * The program should use rand to roll the first die, and
    * should use rand again to roll the second die.

    * The sum of the two values should then be calculated. (Note: Since each die
    * can show an integer value from 1 to 6, then the sum of the two values will vary
    * from 2 to 12 with 7 being the most freqent sum and 2 and 12 being the least frequent
    * sums.) Figure 6.23 shows the 36 possible combinations of the two dice.

    * Your program should
    * roll the two dice 36,000 times. Use a single-scripted array to tally the numbers of times
    * each possible sum appears.

    * Print the results in a tabular format. Also, determine if the totals
    * are resonable; i.e there are six ways to roll a 7, so approximately one sixth of all of the
    * rolls should be 7.
    */
    Here is the code that I currently have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 12
    
    int main()
    {
       int face, face2, face_total, roll1, roll_results[36000], frequency[SIZE]={0};
       
       srand(time(NULL));
       
       for(roll1=1;roll1<=36000; roll1++)
       {
          face = rand() % 6 + 1;
          face2 = rand() % 6 + 1;
          face_total = face + face2;
          roll_results[roll1-1]=face_total;
       }
       
       for(face=1; face<=36000; face++)
       {
          ++frequency[roll_results[face]];
       }
       
       printf("%s%17s\n", "Total", "Frequency");
       
       for(face=1; face<=SIZE; face++)
          printf("%4d%17d\n", face, frequency[face]);
      
       
       system("PAUSE");
       return 0;
    }
    Whenever I run the code, the total number of results is always larger than 36000. The problem states that I should have only 36000 results. Could anyone shed some light. Your help is greatly appreciated.

    Thanks.

    --Kiaiviper

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    One thing I noticed from looking at this quickly is that your array is going out of bounds. Array indexes start at zero, so this line:
    Code:
    for(roll1=1;roll1<=36000; roll1++)
    Should be changed to this:
    Code:
    for(roll1=0;roll1<36000; roll1++)
    When your array size is 36000.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, it is not going out of bounds there (accessed by roll_results[roll1-1], not roll_results[roll1]), but in the later two loops.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Sorry there it wasent, the code just wasent as I expected it. But here it is:
    Code:
       for(face=1; face<=36000; face++)
       {
          ++frequency[roll_results[face]];
       }
    Edit: Oops, yeah seems like you already realized that.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, all the problems came from arrays going out of bounds. Heres a fixed version with some debugging stuff left in there:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 12
    #define ITERATIONS 36000
    
    int main()
    {
       int face, face2, face_total, roll1, roll_results[ITERATIONS], frequency[SIZE+1]={0};
       int tot=0;
       srand(time(NULL));
       
       for(roll1=0;roll1<ITERATIONS; roll1++)
       {
          face = rand() % 6 + 1;
          face2 = rand() % 6 + 1;
          face_total = face + face2;
          roll_results[roll1]=face_total;   
       }
       
       for(face=0; face<ITERATIONS; face++)
          ++frequency[roll_results[face]];
       
       printf("%s%17s\n", "Total", "Frequency");
       
       for(face=0; face<SIZE; face++)
       {
          printf("%4d%17d\n", face+1, frequency[face+1]);
          tot+=frequency[face+1];
       }   
      
       printf("\nTotal values:%i\n", tot);
       system("PAUSE");
       return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    19
    Thank you all for your help and responses. I have reviewed my array declarations and implementations. The program is working now as expected.


    Thank you.


    --KiaiViper

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Your code doesn't meet the specifications of the assignment.

    You don't need an array of 36000. You need an array of size 36 integers, each index value represents the frequency of rolls that matched that index value. Just wanted to say your code is completely wrong given the assignement specifications I saw. /shrug.

    Ie:

    UNSIGNED INT ARRAY FREQ [SIZE 32]

    RUN THIS LOOP X TIMES

    ROLL 2 DICE
    ADD RESULTS

    FREQ[RESULT] = FREQ{RESULT] + 1

    END LOOP

    you've got 2 loops that run 72000 iterations that should get the results in 36,000 iterations
    Last edited by MacNilly; 07-21-2007 at 01:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Sort indexes of frequency array?
    By davidol in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 09:04 AM