code a (C++) program that simulates the throwing of a pair of dice (2 die). Each
ID: 3674076 • Letter: C
Question
code a (C++) program that simulates the throwing of a pair of dice (2 die).
Each die has 6 sides, thus a number between 1 and 6 would represent a die.
You want to show what happens when the dice are thrown 500 times.
There are 11 possible throws (2, 3, 4, 5, 6, 7, 8, 9 10, 11, 12)
You will set up counters to show how many times 7 was thrown, how many times 2 was thrown and how many times doubles (same two die) was thrown.
Print the counters with labels. such as
"7 was thrown 100 times."
(RUN THREE TIMES)
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int i=1;
while(i <=3){// tree times
cout<<"############################"<<endl;
int count[12] = {0}; // initializing with 0
cout<<count[11]<<" "<<count[7];
for(int j=1; j<=500; j++){
int dice1 = (rand() % 6 + 1); // first dice roll
int dice2 = (rand() % 6 + 1); // second dice roll
count[dice1+dice2-1]++; // storing count of sum of numbers
}
for(int k=1; k<12; k++){
cout<<(k+1)<<" was thrown "<<count[k]<<" times"<<endl;
}
cout<<"###################################"<<endl;
i++;
}
return 0;
}
/*
Output:
############################
0 02 was thrown 15 times
3 was thrown 19 times
4 was thrown 35 times
5 was thrown 54 times
6 was thrown 75 times
7 was thrown 91 times
8 was thrown 71 times
9 was thrown 49 times
10 was thrown 52 times
11 was thrown 28 times
12 was thrown 11 times
###################################
############################
0 02 was thrown 11 times
3 was thrown 32 times
4 was thrown 32 times
5 was thrown 54 times
6 was thrown 69 times
7 was thrown 95 times
8 was thrown 75 times
9 was thrown 65 times
10 was thrown 32 times
11 was thrown 26 times
12 was thrown 9 times
###################################
############################
0 02 was thrown 13 times
3 was thrown 21 times
4 was thrown 41 times
5 was thrown 56 times
6 was thrown 80 times
7 was thrown 87 times
8 was thrown 63 times
9 was thrown 61 times
10 was thrown 42 times
11 was thrown 26 times
12 was thrown 10 times
###################################
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.