Question

In: Computer Science

. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum...

. 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).

Solutions

Expert Solution

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;

}


Related Solutions

Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the total on the dice comes up to be a given number. Ask the user for the number that you are rolling for. To have your program roll two dice, use the rand() function this way: die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; Your program then computes and prints the number of rolls it takes to get the...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask the user if they want to roll again. If they don't want to roll again, the program should end. If they do want to roll again, the new number should display. At a minimum, the die should have 6 sides, but if you want to use a d12 or d20, that is fine too. USE PYTHON
Find the probability of rolling a sum of two dice that is a 7 or a...
Find the probability of rolling a sum of two dice that is a 7 or a 12. Round answer to 4 decimal places.
Consider the experiment of rolling two dice and the following events:     A: 'The sum of the...
Consider the experiment of rolling two dice and the following events:     A: 'The sum of the dice is 8' and  B: 'The first die is an odd number' and C:  "The difference (absolute value) of the dice is 2" Find  (a)  p(A and B) (HINT: You cannot assume these are independent events.)        (b)  p(A or B)         (c)  Are A and B mutually exclusive events? Explain. (d)   Are A and B independent events? Explain. (e)   Are B and C independent events? Explain.
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
write c program to generate 6 numbers to simulate rolling of a dice . use while...
write c program to generate 6 numbers to simulate rolling of a dice . use while loop to run 100 and 10000 times. print out how many times it generates 1, 2,3,4,5,6.
Q1. Two fair dice are rolled. What is the probability of… a)         Rolling a sum of...
Q1. Two fair dice are rolled. What is the probability of… a)         Rolling a sum of 4 or doubles? b) Rolling a sum of 4 and doubles c)         Rolling a sum of 2, 4 times in a row? Q2. True or False. A discrete sample space is one in which outcomes are counted
Consider the experiment of rolling two dice. You win if you roll a sum that is...
Consider the experiment of rolling two dice. You win if you roll a sum that is at least 7 and at most 12. Find the probability of a win.
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
1. Suppose you perform an experiment that consists of rolling two dice and recording their sum....
1. Suppose you perform an experiment that consists of rolling two dice and recording their sum. a. What is the sample space of this experiment? b. Find the probability that the sum is either even or more than 9. c. Find the probability that the sum is odd and a multiple of 3. d. Find the expected sum. 2. When Fernando took MAT 257 last semester, he got the same exact score, 85%, in both Test 1 and Test 2....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT