Question

In: Computer Science

Write a program to simulate rolling a six-sided fair die. Allow the user to enter the...

Write a program to simulate rolling a six-sided fair die. Allow the user to enter the

number of rolls.

Your program should use rand() to get the result of die rolling.

Compute the number of occurrences for each number roll.

Calculate the percentage for each number roll.

Out put the results in the following format.

Use a do while loop to allow the user to repeat the process.

Here is the sample run for your program:

Please enter the number of die rolls

25

Number Frequency Percentage

1 3 0.12

2 6 0.24

3 10 0.40

4 1 0.04

5 3 0.12

6 2 0.08

Do you want to continue rolling die another time? Press Y or N

Y

Please enter the number of die rolls

10

I want to be able to do it with a switch statement but am unsure

Solutions

Expert Solution

Code-

#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{   
//generating random variable
srand(time(0));
//variable declare
int n,min =1 ,max = 6,randNo,i;
int a[6] ;
char choice;
//do while loop
do{
//initialize array a[i] to 0
for(i=0;i<6;i++){
a[i]=0;
}
//ask user number of die rolls he want to roll
cout<<"Please enter the number of die rolls"<<endl;
cin>>n;
//generating random number and storing value in a[] array
for(i = 0 ; i < n ;i++){
randNo = min + rand() % (( max + 1 ) - min);
switch(randNo){
case 1 : a[0]++;break;//if randon number generated is 1 than increment a[0]++
case 2 : a[1]++;break;//if randon number generated is 2 than increment a[1]++
case 3 : a[2]++;break;//if randon number generated is 3 than increment a[2]++
case 4 : a[3]++;break;//if randon number generated is 4 than increment a[3]++
case 5 : a[4]++;break;//if randon number generated is 5 than increment a[4]++
case 6 : a[5]++;break;//if randon number generated is 6 than increment a[5]++
}
}
//printintg result
for(i=0;i<6;i++){
cout<<(i+1)<<" "<<a[i]<<" "<<(float)(a[i]/25.0)<<endl;
}
cout<<"Do you want to continue rolling die another time? Press Y or N"<<endl;
cin>>choice;
}while(choice!='N');
return 0;
}

Screenshots -


Related Solutions

Suppose you are rolling a fair four-sided die and a fair six-sided die and you are...
Suppose you are rolling a fair four-sided die and a fair six-sided die and you are counting the number of ones that come up. a) Distinguish between the outcomes and events. b) What is the probability that both die roll ones? c) What is the probability that exactly one die rolls a one? d) What is the probability that neither die rolls a one? e) What is the expected number of ones? f) If you did this 1000 times, approximately...
The experiment of rolling a fair six-sided die twice and looking at the values of the...
The experiment of rolling a fair six-sided die twice and looking at the values of the faces that are facing up, has the following sample space. For example, the result (1,2) implies that the face that is up from the first die shows the value 1 and the value of the face that is up from the second die is 2. sample space of tossing 2 die A pair of dice is thrown. Let X = the number of multiples...
Consider rolling both a fair four-sided die numbered 1-4 and a fair six-sided die numbered 1-6...
Consider rolling both a fair four-sided die numbered 1-4 and a fair six-sided die numbered 1-6 together. After rolling both dice, let X denote the number appearing on the foursided die and Y the number appearing on the six-sided die. Define W = X +Y . Assume X and Y are independent. (a) Find the moment generating function for W. (b) Use the moment generating function technique to find the expectation. (c) Use the moment generating function technique to find...
1. The experiment of rolling a fair six-sided die twice and looking at the values of...
1. The experiment of rolling a fair six-sided die twice and looking at the values of the faces that are facing up, has the following sample space. For example, the result (1,2) implies that the face that is up from the first die shows the value 1 and the value of the face that is up from the second die is 2. (1,1)       (1,2)       (1,3)       (1,4)       (1,5)       (1,6) (2,1)       (2,2)       (2,3)       (2,4)       (2,5)       (2,6) (3,1)       (3,2)       (3,3)       (3,4)       (3,5)       (3,6)...
Rolling a fair six-sided die five times could result in the following sample of n =...
Rolling a fair six-sided die five times could result in the following sample of n = 5 observations: What are the mean, variance, and standard deviation?
Consider the experiment of rolling a six-sided fair die. Let X denote the number of rolls...
Consider the experiment of rolling a six-sided fair die. Let X denote the number of rolls it takes to obtain the first 5, Y denote the number of rolls until the first 2, and Z denote the number of rolls until the first 4. Numerical answers are needed only for parts (a) and (b). Expressions are sufficient for parts (c), (d), and (e). a) E[X|Y = 1 or Z = 1] b) E[X|Y = 1 and Z = 2] c)...
Let X be the result of rolling a fair ten-sided die a) Write down the distribution...
Let X be the result of rolling a fair ten-sided die a) Write down the distribution of X, and compute its expected value. You must show all your work. b) How would you use R to simulate 4 rolls of ten-sided die and sum the result, then repeat this action 10,000 times, and store it in a variable called toss4. Please type the code in the space below.
Suppose a red six-sided die, a blue six-sided die, and a yellow six-sided die are rolled....
Suppose a red six-sided die, a blue six-sided die, and a yellow six-sided die are rolled. Let - X1 be the random variable which is 1 if the numbers rolled on the blue die and the yellow die are the same and 0 otherwise; - X2 be the random variable which is 1 if the numbers rolled on the red die and the yellow die are the same and 0 otherwise; - X3 be the random variable which is 1...
Write a C++ program that : 1. Allow the user to enter the size of the...
Write a C++ program that : 1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11. 2. Create an vector of size N x N. 3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array. 4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix...
Consider a fair four-sided die numbered 1-4 and a fair six-sided die numbered 1-6, where X...
Consider a fair four-sided die numbered 1-4 and a fair six-sided die numbered 1-6, where X is the number appearing on the four-sided die and Y is the number appearing on the six-sided die. Define W=X+Y when they are rolled together. Assuming X and Y are independent, (a) find the moment generating function for W, (b) the expectation E(W), (c) and the variance Var(W). Use the moment generating function technique to find the expectation and variance.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT