In: Computer Science
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] The following table shows the 36 possible combinations of the two dice. Your program should roll the two dice 36,000,000 times. Use a onedimensional 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 six ways to roll a 7, so approximately one-sixth of all the rolls should be 7).
Given below is the code for the question. Please do rate the answer if it helped. Thank you
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
class Die{
private:
int value;
public:
Die(){
roll();
}
int getValue(){
return value;
}
int roll(){
value = 1+ rand() % 6;
return value;
}
};
int main(){
srand(time(0));
Die d1, d2;
int sum[12] = {0};
unsigned long n = 36000000;
int s;
double perc;
for(int i = 1; i <= n; i++){
s = d1.roll() + d2.roll();
sum[s-1]++;
}
cout << "Sum \t Frequency \t Percentage" <<
endl;
cout << fixed << setprecision(2) << endl;
for(int i = 2; i <= 12; i++){
perc = sum[i-1] * 100.0 / n;
cout << i << " \t " << sum[i-1] << " \t "
<< perc << " %" << endl;
}
cout <<endl << "Total no. of rolls = " << n << endl;
}