Question

In: Computer Science

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 given number. Valid numbers are 2 through 12.

If the user asked for a 2 your program should output:

It took 20 rolls to get a 2.

(your number of rolls will be different of course)

Solutions

Expert Solution

//program for given question

#include <iostream>
#include <stdlib.h> //for srand(), rand()
#include <time.h> //for time()

using namespace std;

int main()
{
int found=0,num,count=0;
int die1,die2;
srand (time(NULL)); //initializing random seed
cout<<"The number that you are rolling for: "; //taking input from user
cin>>num;
if(num>12 || num<2){ //checking for valid input
cout<<"INVALID NUMBER!!(Number should be from 2 through 12)";
return 0; //if the input is not valid program will terminate here.
}
while(found==0){ //if input is valid this loop will execute
count++; //counting number of times this loop runs
die1 = rand() % 6 + 1; //rolling dice 1
die2 = rand() % 6 + 1; //rolling dice 2
if((die1+die2)==num){ //checking if we got the required number
found=1;
}
}
cout<<"It took "<<count<<" rolls to get a "<< num; //giving the output
  
return 0; //end of program
}

NOTE- In this program we are using srand to initializing random seed .If we don't use srand then the program will genrate same sequence of numbers every time it is executed .

for eg: If the user gives input 6 then supoose it take 8 rolls of dice to get 6 .Then if you run the program again with same input 6 then it will again give same output that is 8 rolls of dice .

so to avoid this we use srand() with time() function to generate a randome seed.

CODING AND OUTPUT SCREEN

OUTPUT:


Related Solutions

. 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...
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
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided dice that number of times. The program should keep track of how many times each possible sum comes up using a list. The list's first element should contain how many times a total of 2 was rolled, the second should contain how many times a total of 3 was rolled, and so on all the way through to 12. When all rolls are complete,...
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.
Consider rolling a fair dice. You keep rolling the dice until you see all of the...
Consider rolling a fair dice. You keep rolling the dice until you see all of the faces (from number 1 to 6) at least once. What is your expected number of rolls?
I. Consider the random experiment of rolling a pair of dice. Note: Write ALL probabilities as...
I. Consider the random experiment of rolling a pair of dice. Note: Write ALL probabilities as reduced fractions or whole numbers (no decimals). 1) One possible outcome of this experiment is 5-2 (the first die comes up 5 and the second die comes up 2). Write out the rest of the sample space for this experiment below by completing the pattern: 1-1 2-1 1-2 1-3 1-4 1-5 1-6 2) How many outcomes does the sample space contain? _____________ 3) Draw...
I. Consider the random experiment of rolling a pair of dice. Note: Write ALL probabilities as...
I. Consider the random experiment of rolling a pair of dice. Note: Write ALL probabilities as reduced fractions or whole numbers (no decimals). 1-1 2-1 3-1 4-1 5-1 6-1 1-2 2-2 3-2 4-2 5-2 6-2 1-3 2-3 3-3 4-3 5-3 6-3 1-4 2-4 3-4 4-4 5-4 6-4 1-5 2-5 3-5 4-5 5-5 6-5 1-6 2-6 3-6 4-6 5-6 6-6 2) How many outcomes does the sample space contain? _____36________ 3)Draw a circle (or shape) around each of the following events...
Write a C program called cards.c that simulates some card game logic by comparing the cards...
Write a C program called cards.c that simulates some card game logic by comparing the cards from 4 people and determining which person has the best card. The program MUST work as follows: Eachcardmustberepresentedbyexactlytwocharsrepresenting a rank and a suit. The possible rank options are: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. The possiblesuit options are: 'H', 'D', 'S', 'C'. So 6H represents the “6 of hearts”, JC represents the “Jack of Clubs” etc... YouMUSTwriteafunctioncalledisValidRank(charc)which...
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...
Write a program that simulates flipping a coin repeatedly until three consecutive        heads are...
Write a program that simulates flipping a coin repeatedly until three consecutive        heads are tossed. The program should then display the total number of times the coin was        flipped. The user does not have to enter any information and we must use a while loop.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT