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:
Here is the code that I currently have://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.
*/
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.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; }
Thanks.
--Kiaiviper