In: Computer Science
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
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;
}
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;
}
================================================================