Write a program that simulates the rolling of two dice. The program should use r
ID: 3806373 • Letter: W
Question
Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and again use it to roll the second die. The sum of the two dice should be calculated and then printed in a tabular form. Note that the results should vary from 2 to 12 with 2 being the least frequent as there is one way only to get 2 and 7 being the most frequent as there is six ways to get 7. Execute and submit the program and the results in screen captures.
a) Verify that your results are reasonable in that the 7 should appear 6/36 of the times the dice are thrown, and the 2 should appear 1/36 of the times. Determine the theoretical frequency (just like I stated above) and the actual frequency the program outputs.
b) Increase the number of times the dice are thrown and rerun the program and comment on how the results changed.
In C,C++
Explanation / Answer
Please refer below code
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
//generate between 1 and 6
int get_random()
{
return (rand() % 6 + 1);
}
int main()
{
int outcome[13] = {0};
int dice1,dice2;
srand(time(0));
cout<<"Dice 1 "<<"Dice 2 "<<"Total Score"<<endl;
for(int i = 0; i < 36; i++)
{
dice1 = get_random();
dice2 = get_random();
int sum = dice1 + dice2; //calculating sum of scores of both dice
outcome[sum]++;
cout<<dice1<<" "<<dice2<<" "<<sum<<endl;
}
cout<<"Sum of Score : ";
for(int i = 2; i <= 12; i++) //for loop for sum of score
cout<<i<<" ";
cout<<endl;
cout<<"Frequency : ";
for(int i = 2; i <= 12; i++) //for loop for frequency of occuerence of sum of scores
cout<<outcome[i]<<" ";
return 0;
}
Please refer below output
Dice 1 Dice 2 Total Score
1 4 5
2 5 7
2 1 3
4 6 10
1 5 6
1 6 7
4 6 10
1 1 2
3 1 4
3 1 4
5 3 8
4 3 7
4 1 5
1 4 5
6 5 11
6 4 10
4 3 7
2 1 3
4 4 8
4 6 10
4 4 8
5 4 9
4 2 6
1 3 4
1 6 7
6 5 11
6 3 9
2 4 6
6 5 11
3 6 9
5 3 8
5 2 7
1 4 5
6 3 9
6 4 10
5 2 7
Sum of Score : 2 3 4 5 6 7 8 9 10 11 12
Frequency : 1 2 3 4 3 7 4 4 5 3 0
Process returned 0 (0x0) execution time : 0.040 s
Press any key to continue.
In above code you can clearly see that 7 occurres most of the time.If you repeatedly run this code then the occuerence may vary but most of the time you will get frequcy of 7 more.
b)
In this code I made throw count to 72(earlier it was 36) and getting below results
code:
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
//generate between 1 and 6
int get_random()
{
return (rand() % 6 + 1);
}
int main()
{
int outcome[13] = {0};
int dice1,dice2;
srand(time(0));
cout<<"Dice 1 "<<"Dice 2 "<<"Total Score"<<endl;
for(int i = 0; i < 72; i++)
{
dice1 = get_random();
dice2 = get_random();
int sum = dice1 + dice2; //calculating sum of scores of both dice
outcome[sum]++;
cout<<dice1<<" "<<dice2<<" "<<sum<<endl;
}
cout<<"Sum of Score : ";
for(int i = 2; i <= 12; i++) //for loop for sum of score
cout<<i<<" ";
cout<<endl;
cout<<"Frequency : ";
for(int i = 2; i <= 12; i++) //for loop for frequency of occuerence of sum of scores
cout<<outcome[i]<<" ";
return 0;
}
output
Dice 1 Dice 2 Total Score
2 2 4
5 2 7
1 1 2
2 2 4
6 2 8
2 6 8
6 2 8
4 5 9
6 1 7
6 6 12
3 1 4
5 2 7
5 3 8
5 6 11
2 2 4
5 5 10
1 3 4
3 5 8
2 4 6
3 3 6
4 6 10
3 5 8
4 2 6
1 3 4
5 5 10
5 6 11
1 2 3
1 5 6
3 5 8
5 4 9
2 4 6
2 1 3
1 1 2
1 5 6
2 3 5
3 2 5
3 1 4
3 6 9
6 1 7
5 5 10
6 2 8
4 6 10
1 6 7
3 6 9
2 5 7
6 5 11
2 5 7
3 6 9
5 1 6
4 4 8
1 3 4
6 6 12
1 5 6
2 6 8
3 6 9
4 4 8
3 1 4
4 1 5
5 4 9
5 4 9
4 2 6
2 1 3
6 3 9
6 2 8
5 1 6
4 5 9
4 3 7
4 5 9
1 5 6
4 5 9
3 5 8
5 4 9
Sum of Score : 2 3 4 5 6 7 8 9 10 11 12
Frequency : 2 3 9 3 11 8 13 13 5 3 2
Process returned 0 (0x0) execution time : 0.262 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.