Question

In: Computer Science

Modify the code below to support specifying additional dice type in addition to a 6 sided...

Modify the code below to support specifying additional dice type in addition to a 6 sided dice.

For example, the program could support six-sided, eight-sided, 10 sided die.

HINTS: You will need to

  • Create an enum type for the dice type and values
  • Modify the parameter passing of some of the functions to pass the die type
  • Adjust the actual code that simulates the die roll to generate a range of values appropriate to the user's selected die type.

Think about parameter passing. It is possible to do this with a very few modifications to the program.

//Program: Roll dice

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice(int num);

int main()
{
cout << "The number of times the dice are rolled to "
<< "get the sum 10 = " << rollDice(10) << endl;
cout << "The number of times the dice are rolled to "
<< "get the sum 6 = " << rollDice(6) << endl;

return 0;
}

int rollDice(int num)
{
int die1;
int die2;
int sum;
int rollCount = 0;

srand(time(0));

do
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
rollCount++;
}
while (sum != num);

return rollCount;
}

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
enum DICE_TYPE {SIX=6, EIGHT=8, TEN=10};

int rollDice(int num, DICE_TYPE);


int main()
{
  
DICE_TYPE type;
int sides;
cout<<"Enter Dice type (1)Six-Sides, (2)Eight-Sides, (3)Ten-Sides: "; cin>> sides;
if(sides==1) type = SIX;
else if(sides==2) type =EIGHT;
else if(sides==3) type = TEN;
cout << "The number of times the dice are rolled to get the sum 10 = " << rollDice(10,type) << endl;
cout << "The number of times the dice are rolled to get the sum 6 = " << rollDice(6,type) << endl;

return 0;
}

int rollDice(int num, DICE_TYPE type)
{
int die1;
int die2;
int sum;
int rollCount = 0;

srand(time(0));

do
{
   die1 = rand() % type + 1;
   die2 = rand() % type + 1;
   sum = die1 + die2;
   rollCount++;
}
while (sum != num);

return rollCount;
}

================================================================


Related Solutions

(5) A fair 6-sided dice is rolled twice. The fairness of the dice consists of the...
(5) A fair 6-sided dice is rolled twice. The fairness of the dice consists of the following facts: (i) no face of the dice is likelier to be rolled than any other (so a roll of a 5 and a roll of a 3 are equiprobable) and (ii) the dice has no “memory” - each roll is independent of any other. 5.1: What is the probability that you’ve first rolled a 3 and the pair of rolls sums to something...
One red (6 sided) and one white (6 sided) dice is rolled. The random variable X...
One red (6 sided) and one white (6 sided) dice is rolled. The random variable X has the value 1 if the red dice shows a number max 2 and is 2 else. The random variable y has the value 1 if the white dice shows a uneven number and is 2 else. A) determine the common probability function (x, y) B) determine the marginal probability functions of x and y C) calculate e(x), e(x^2), var(x), e(y), e(y^2), and var(y)
Modify the original code and add an additional function of your choice. The function should be...
Modify the original code and add an additional function of your choice. The function should be unique and something you created for this assignment. Support your experimentation with screen captures of executing the new code. Prepare a test table with at least 3 distinct test cases listing input and expected output for your unique function. #include <stdio.h> void printHelp () { printf ("\n"); printf ("a: a(x) = x*x\n"); printf ("b: b(x) = x*x*x\n"); printf ("c: c(x) = x^2 + 2*x...
You roll two 6-sided dice numbered 1 through 6. Let A be the event that the...
You roll two 6-sided dice numbered 1 through 6. Let A be the event that the first die shows the number 3, let B be the event that the second die shows a 5, and let E be the event that the sum of the two numbers showing is even. Compute P(A)and P(B)and then compute P(AlB). What does this tell you about events A and B?Hint: Remember that the sample space has 36 outcomes! Compute P(ElA)and compute P(E). What does...
. Three Dice of a Kind Consider the following game: You roll six 6-sided dice d1,…,d6...
. Three Dice of a Kind Consider the following game: You roll six 6-sided dice d1,…,d6 and you win if some number appears 3 or more times. For example, if you roll: (3,3,5,4,6,6) then you lose. If you roll (4,1,3,6,4,4) then you win. What is the probability that you win this game?
Two 6-sided dice are rolled. One die is a standard die, and the other is a...
Two 6-sided dice are rolled. One die is a standard die, and the other is a Fibonacci die with sides 1, 1, 2, 3, 5, and 8. a. What is the probability distribution of this experiment? b. What is the shape of the probability distribution? c. What is the expected value when these 2 dice are rolled?
So, roll a fair 6-sided dice once, and if the result is 1,2,3, or 4 then...
So, roll a fair 6-sided dice once, and if the result is 1,2,3, or 4 then toss a fair coin 3 times. If the first result is 5, 6, then toss a fair coin until two tails show up. Then, what is the expected value of number of heads? PLZ help me with this!!! THX soooooo much!
Bobby rolls 2 fair 6-sided dice and considers it a "success" if the sum of the...
Bobby rolls 2 fair 6-sided dice and considers it a "success" if the sum of the die results is 5 or 6. He performs 4 trials.   Martha tosses 2 fair coins and considers it a "success" if both coins land on "heads." She performs 3 trials. Heather rolls 1 fair 4-sided die and considers it a "success" if he rolls a "1." He performs 5 trials. What is the probability that exactly 2 success occur in all of the combined...
Consider the game consisting of rolling a pair of fair 6-sided dice and recording the sum....
Consider the game consisting of rolling a pair of fair 6-sided dice and recording the sum. It will cost you $1 to play the game. If the sumis less than 5, then you will win $3. However, you do not get your $1 back so your profit is $2. If you roll a sum of exactly8, thenyou will win $4. However, you do not get your $1 back so your profit is $3. Otherwise, you lose your $1. A.What is...
Alice rolls a pair of fair six-sided dice until a sum of 6 appears for the...
Alice rolls a pair of fair six-sided dice until a sum of 6 appears for the first time. Independently, Bob rolls two fair six-sided dice until he rolls a sum 7 for the first time. Find the probability that the number of times Alice rolls her dice is equal to or within one of the number of times Bob rolls his dice.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT