write a program that simulates the rolling of two dice. the program should use r
ID: 3546482 • Letter: W
Question
write a program that simulates the rolling of two dice. the program should use rand() to roll the first dice and should use rand() again to roll the second dice. the sum of the two values should then be calculated. Note: since each dice can show an integer value from 1 to 6, then the sum of the two values will vary from 1 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. your program should roll the two dice 36.000 times. use a single-dimensional array frequency to tally the number of times each possible sum appears. for example, after making 36.000 experiments. frequency[0] should store the number of times when the sum was equal to 2. frequency[1] should store the number of times when the sum equal to 3. and so on. then use another single-dimensional array prob to store the porbability of getting each possible sum. i.e., prob[0] should store the probability that the sum is equal to 2. prob [1] should store the probability that the sum is equal to 3, and so on. To calculate the i-th element of prob. use the formula: prob [1] = frequency [1]/36,000. Display both arrays' frequency and prob in a tabular format.
Explanation / Answer
// ConsoleApplication7.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
int _tmain(int argc, _TCHAR* argv[])
{
//Initialize the rand function
srand (time(NULL));
int frequency[11];
double prob[11];
for(int i=0; i<11; i++)
{
frequency[i] = 0;
prob[i] = 0;
}
for(int i=0; i<36000; i++)
{
int dice1 = rand()%6 + 1;
int dice2 = rand()%6 + 1;
int sum = dice1 + dice2;
frequency[sum - 2]++;
}
printf("The frequency table is ");
for(int i=1; i<11; i++)
{
printf("%d ",frequency[i]);
prob[i] = (double)frequency[i] / (double)36000;
}
printf(" The probability table is ");
for(int i=1; i<11; i++)
printf("%lf ",prob[i]);
getchar();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.