Frequency Array

This is a discussion on Frequency Array within the C Programming forums, part of the General Programming Boards category; Good Afternoon, I am working on a problem from the Dietel & Dietel C Programming Book. I have solved the ...

  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
    22,159
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    326
    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 02: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, 11: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, 05:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Sort indexes of frequency array?
    By davidol in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 10:04 AM

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