Question

please do it in C++ Write a program that simulates the rolling of two dice. The...

please do it in C++
Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. 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 2and 12 being the least frequent sums. The image below shows the 36 possible combinations of the two dice. Your program should roll the two dice 36,000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Print the results in 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).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code::

C++

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

using namespace std;

int main() {

/* initialize random seed: */
srand (time(NULL));
  
//to store sum between 2 to 12
int count[13]={0};
  
for (int start = 0; start < 35000; start++) {
/* generate secret number between 1 and 6: */
  
int firstdice,seconddice;
  
firstdice = rand() % 6 + 1;
  
seconddice= rand() % 6 + 1;
count[firstdice+seconddice]++;
}
  
for (int start=2; start<=12; start++)
{
  
cout<<"Sum "<<start<<" Occurs " <<count[start]<<"\n";
}
cout<<"\n";
/*
The frequencies are stored in count. and sum of them is definitely 36000.
when you want to show that they are reasonable you evaluate count[i]/36000. they must be close to the numbers I wrote in the previous reply.
count[2]/36000 ~ 1/36
count[3]/36000 ~ 2/36
.
.
.
count[11]/36000 ~ 2/36
count[12]/36000 ~ 1/36*/


for (int start=2; start<=12; start++)
{
  
cout<<"Freq "<<start<<" Occurs " <<float(count[start])/36000<<"\n";
  
  
}

 #include <cstdlib> #include <ctime> #include <iostream> using namespace std; int main() { /* initialize random seed: */ srand (time(NULL)); //to store sum between 2 to 12 int count[13]={0}; for (int start = 0; start < 35000; start++) { /* generate secret number between 1 and 6: */ int firstdice,seconddice; firstdice = rand() % 6 + 1; seconddice= rand() % 6 + 1; count[firstdice+seconddice]++; } for (int start=2; start<=12; start++) { cout<<"Sum "<<start<<" Occurs " <<count[start]<<"\n"; } cout<<"\n"; /* The frequencies are stored in count. and sum of them is definitely 36000. when you want to show that they are reasonable you evaluate count[i]/36000. they must be close to the numbers I wrote in the previous reply. count[2]/36000 ~ 1/36 count[3]/36000 ~ 2/36 . . . count[11]/36000 ~ 2/36 count[12]/36000 ~ 1/36*/ for (int start=2; start<=12; start++) { cout<<"Freq "<<start<<" Occurs " <<float(count[start])/36000<<"\n"; } } 


OUTPUT:

Result CPU Time: 0.00 sec(s), Memory: 3348 kilobyte(s) compiled and executed in 1.469 sec(s) Sum Sum 2 3 4 5 Sum 6 Sum Sum Su
  
}

Add a comment
Know the answer?
Add Answer to:
please do it in C++ Write a program that simulates the rolling of two dice. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program that simulates the rolling of four dice. The program should use random...

    Write a java program that simulates the rolling of four dice. The program should use random number generator to roll dice. The sum of the four values should then be calculated. Note that each die can show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the least frequent sums. Your program should roll the dice 12,960...

  • ( Java Programming ) Write an application to simulate the rolling of two dice. The application...

    ( Java Programming ) Write an application 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, with 7 being the most frequent sum, and 2 and 12...

  • 1T (startIndex< size) ( 22 23 someFunction(b, startIndex +1, size); printf("%d ", b[startIndex]); 24 25 26...

    1T (startIndex< size) ( 22 23 someFunction(b, startIndex +1, size); printf("%d ", b[startIndex]); 24 25 26 (Dice Rolling) Write a program that simulates the rolling of two dice. The program should rand twice to roll the first die and second die, respectively. The sum of the two values should then be calculated. [Note: Because each die can show an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12, with 7...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • In python 3, Write a program in Python that simulates the roll of two dice. The...

    In python 3, Write a program in Python that simulates the roll of two dice. The first die is a 6-sided die. The second die is an 8-sided die. Generate a random number for a die and store it in variable number1. Generate a random number for a die and store it in variable number2. Note: Submit your code with the print("You rolled two dice:", number1, "and", number2) statement.

  • Problem 9 A single game of craps (a dice game) consists of at most two rolls...

    Problem 9 A single game of craps (a dice game) consists of at most two rolls of a pair of six sided dice. The ways to win are as follows: Win-the first roll of the pair of dice sums to either 7 or 1 (you win, game over, no second roll Win the first roll of the pair of dice does NOT sum to either 7 or 1 but the sum of the second roll is equal to the sum...

  • (MATLAB) Write a program that simulates a dice roll by picking a random number from 1-6...

    (MATLAB) Write a program that simulates a dice roll by picking a random number from 1-6 and then picking a second random number from 1-6.    How many times do you get two 1’s. What many times do you get two 1’s if you simulate 10,000 rolls of a pair of dice?

  • Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In...

    Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make certain combinations. For example, the Yahtzee combination is formed by all five dice having the same value. A large straight occurs when all of the dice are in consecutive order (e.g., 1,2,3,4,5 or 2,3,4,5,6). If you haven’t played Yahtzee, you can find out more from various online sources such as Wikipedia. Once...

  • Dice Rollers( write java) Write a complete thread class to represent rolling dice. The thread tak...

    Dice Rollers( write java) Write a complete thread class to represent rolling dice. The thread takes a parameter that represents how many dice are being rolled at once and how many times to roll. When the thread is run, it rolls the specified number of dice the specified number of times. The thread counts for how many rolls the number on all dice match. This value is saved as instance data. Write a program to create and run several threads...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT