1. ## 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.

* 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. 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. 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.

4. 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. 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. 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. 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