2. [Programming] [40 points] Write a program that simulates the rolling of three
ID: 3703913 • Letter: 2
Question
2. [Programming] [40 points] 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 an 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 a tabular format. Also, determine if the totals are reasonable; i.e., there are 27 ways to roll a 10, so approximately one-eighth of all the rolls should be 10 C:UsersIzouja IDesktop TwoDiceIDebug ITwoDice.exe Frequency of sun- 3 is 9918, about 0.46% Frequency of sun- 4 is 29805 , about 1.38% Frequency of sun- 5 is 59891, about 2.77% Frequency of sun= 6 is 99990, about 4.63% Frequency of sun 7 is 149689, about 6.93% Frequency of sun 8 is 210083, about 9.73% Frequency of sun 9 is 250166, about 11.58% Frequency of sun -10 is 269497, about 12.48% Frequency of sun- 11 is 270326, about 12.52% Frequency of sun - 12 is 250519, about 11.60% Frequency of sun -13 is 209932, about 9.72% Frequency of sun- 14 is 15R114, about 6.95% Frequency of sun -15 is 108118, about 4.63% Frequency of sun-16 is 59833, about 2.77% Frequency of sun-17 is 29854, about 1.38% Frequency of sun-18 is 10273, about 0.48%Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
int main()
{
int t1,t2,t3;
unsigned int i; // for loop counter
/*intializing all conter as a zero*/
int count[18]={ 0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0};
for(i=0;i<2160000;i++) // looping upto 2160000 time asnd generating 3 random data
{
srand((unsigned) rand()); // feeding random data as a seed
t1 = rand()%6+1; // generating random number, number may be bet ween 0 to MAX. so restrict it between 0 to 6 by % 6 operation. but here don't requre 0. so thats what we are adding + 1 at the end
srand((unsigned) rand());
t2 = rand()%6+1; // same comment as t1
srand((unsigned) rand());
t3 = rand()%6+1; // generatinf random number btwn 1 to 6
// suppose generated number is 1,2,3 then output sum will be 6. so we will increment counter by 1 of 6 number.
// like that incrementing sum counter
count[t1+t2+t3] = count[t1+t2+t3] + 1;
}
// starting from 3 as 0,1,2 sum not possible. calculating % by (avalable_count * 100) / total_count
for(i=3;i<18;i++)
printf("frequency of sum =%2d is %8d times, about %8.2f% ",i,count[i],(float)((float)(count[i] * 100 )/2160000));
}
/*
OUTPUT:
frequency of sum = 3 is 10911 times, about 0.51%
frequency of sum = 4 is 28697 times, about 1.33%
frequency of sum = 5 is 51781 times, about 2.40%
frequency of sum = 6 is 101723 times, about 4.71%
frequency of sum = 7 is 154523 times, about 7.15%
frequency of sum = 8 is 230261 times, about 10.66%
frequency of sum = 9 is 236367 times, about 10.94%
frequency of sum =10 is 268879 times, about 12.45%
frequency of sum =11 is 268538 times, about 12.43%
frequency of sum =12 is 267082 times, about 12.36%
frequency of sum =13 is 207954 times, about 9.63%
frequency of sum =14 is 138174 times, about 6.40%
frequency of sum =15 is 95740 times, about 4.43%
frequency of sum =16 is 55001 times, about 2.55%
frequency of sum =17 is 32279 times, about 1.49%
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.