Suppose you roll a set of n dice. Then the smallest sum is n and the largest sum
ID: 3724230 • Letter: S
Question
Suppose you roll a set of n dice. Then the smallest sum is n and the largest sum is 6n. For example, if n = 10, then the smallest sum is 10 and the largest sum is 60. Let m be the desired sum of the numbers rolled. Then n m 6n. If n = 10, then 10 m 60. Write a program that uses the class die, (Example 10-9 in the book), to roll 10 dice. (Use an array of size 10 to implement 10 dice.)
• The program prompts the user to enter the desired sum and the number of times the dice are to be rolled.
• The program outputs the number of times the desired sum was rolled and the probability of rolling the desired sum.
Test run your program to roll the 10 dice 10000, 100000, 1000000, and 10000000 times with the desired sums 10, 25, 40, and 60. Provide a printed report on how many times each of those sums was rolled in each of the cases. The report should look like this:
Total Rolls Desired Sum 10,000 100,000 1,000,000 10,000,000 10 1698 16782 166232 1666667 25 ... ... ... ... 40 ... ... ... ... 60 ... ... ... ...Explanation / Answer
You have not specified any language. So I am using C++
CPP Code:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
/*Class defination start*/
class Dice{
int dice[10];
public:
/*This function simulates the dice roll's*/
void roll_dice(){
int i;
for(i =0; i < 10; i++)
{
dice[i] = rand() % 6 + 1;
}
}
/*Returns the sum of dice roll's*/
int sum(){
int i;
int sum = 0;
for(i =0; i < 10; i++)
{
sum += dice[i];
}
return sum;
}
};/*End*/
/*Start of main*/
int main()
{
Dice dice;
int desired_sum;
int num_roll;
int i;
int desired_sum_count;
cout << "Please enter desired sum : ";
cin >> desired_sum;
cout << " Please enter number of times you want to roll the dice : ";
cin >> num_roll;
cout << endl;
srand(time(0));
/*Performing experiment*/
for(i =0; i < num_roll; i++)
{
dice.roll_dice();
if (dice.sum() == desired_sum)
desired_sum_count++;
}
cout << "Number of times the desired sum was rolled : " << desired_sum_count << endl;
cout << "The probability of rolling the desired sum : " << ((float)desired_sum_count)/ ((float) num_roll) << endl;
}/*End*/
Output shown after running this program:
./a.out
Please enter desired sum : 25
Please enter number of times you want to roll the dice : 10000
Number of times the desired sum was rolled : 128
The probability of rolling the desired sum : 0.0128
--------------------------------------------------------------------------
i ran this program multiple times and here are the results:
Desired Sum 10,000 100,000 1,000,000 10,000,000
10 0 0 0 0
25 135 1400 13812 137113
40 459 4885 48529 484223
60 0 0 0 0
Please share your feedback and concern if any.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.