Question
C Programming: Write a program that simulates the rolling of three dice. The program should use rand three times to roll the first die, second
C Programming:
Write a program that simulates the rolling of three dice. The program should use rand three times to roll the first die, second die and third die, respectively. The sum of the three values should then be calculated. [Note: Because each die can show and integer value from 1 to 6, then the sum of the three values will vary from 3 to 18, with 10 and 11 being the most frequent sums and 3 and 18 the least frequent sums.] Your program should roll the three dice 2,160,000 times. Use a single-subscripted array to tally the numbers of times each possible sum appears. Print the results in tabular format. Also, determine if the totals are reasonable; i.e., are there 27 ways to roll a 10, so approximately one-eighth of all the rolls should be 10.
1. Pseudocode of the main() function
Define a constant NUM_ROLL = 2160000
Define an array freq with 16 elements for the frequency counters
Initialize all the elements to 0
Repeat the following steps for NUM_ROLL iterations:
Call the function roll_three_dice() to return the sum of the face values of three rolls
Increase the corresponding array element by 1
Print the array to display the results, i.e., the frequency counts and the percentages
Stop
Note: The percentage is given by (double) freq[i] / (double) NUM_ROLL
2. Pseudocode of the function roll_three_dice
It takes no parameters and returns the sum of the face values of three rolls. Its header is
unsigned int roll_three_dice(void)
Pseudocode:
Get the face value from rolling the 1st die by using rand() % 6 + 1
Get the face value from rolling the 2nd die by using rand() % 6 + 1 again
Get the face value from rolling the 3rd die by using rand() % 6 + 1 again
Return the sum of the three face values
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started