Write a program that simulates the rolling of two dice. The program should use r
ID: 3768633 • Letter: W
Question
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. Since each die can show an integer value from 1 to 6, then the sum of the two values will vary from 1 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. Your program should roll the two dice 36,000 times. Use a single-dimensional array frequency to tally the numbers of times each possible sum appears. For example, after making 36,000 experiments, frequency [0] should store the number of times when the sum was equal to 2, frequency [1] should store the number of times when the sum was equal to 3, and so on. Then, use another single-dimensional array prob to store the probability of getting each possible sum, i.e., prob [0] should store the probability that the sum is equal to 2, prob [1] should store the probability that the sum is equal to 3, and so on. To calculate the i-th element of prob, use the formula: prob [i] = frequency [i] /36,000. Display both arrays frequency and prob in a tabular format.Explanation / Answer
Below are some programs try them
1st program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
const int Limit = 6;
void main()
{
int n1, n2, res;
int arr[100];
cout << " *** Roll 2 Dices 100 times Each & Check the sums *** ";
cout << " ******* Check these out ********* ";
for(int n=0; n< 100 ; n++)
{
cout << " YOUR TURN No. = " << n+1 << " ";
// roll the first dice
cout << " Roll the first Dice ";
randomize();
n1 = random(Limit);
// roll the second dice
cout<< " Roll the sec dice :";
randomize();
n2 = random(Limit);
// sum of the drawn result
res = n1 + n2;
// store the sum in an array
arr[n] = res;
}
cout << "Now ,Guess the sum of the result of the rolling two dices~ "
// Array for Guess No.
int g[100];
for( n=0 ; n< 100; n++)
{
cout<< " Predict The Sum For Turn : " << n+1;
cout << " Enter your number ( between 2 - 12) :";
cin >> g[n];
}
cout << " If the guess number and actual number matches You WON ";
cout<< " ***** TABLE ***** ";
cout << " TURN SUM GuessNo. RESULT? ";
// Print the result in tabular format
for( n=0 ; n< 100; n++)
{
// if guess no is equal to the sum , then WON , else LOST
if(g[n] == arr[n])
cout << " " << n+1 << " " << arr[n] << " " << g[n] << " " << "WON by guessing";
else
cout << " " << n+1 << " " << arr[n] << g[n] << "LOST by guessing";
// print 15 to 20 records at a time
if ( n == 15 || n == 35 || n == 55 || n == 75 )
{
cout << " Press any key to continue :";
getch();
clrscr();
}
}
getch();
}
2nd program
3rd program
#include <iostream>
using namespace std;
#include <iomanip>
#include <cstdlib>
#include <ctime>
int main()
{
const int arraysize = 13;
int counter[13];
// init counter
for(int i=0; i<13; i++)
counter[i] = 0;
int die1;
int die2;
srand( time( 0 ) );
for ( int roll1 = 0; roll1 <=36000; roll1++ )
{
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
counter[die1+die2]++;
}
cout<<"sum of Faces"<<setw(13)<<"Frequency"<<endl;
for(int face=2; face<arraysize;face++)
cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;
return 0;
}
for the second part:
we have 36 possibilities for dies
1 + 1 = 2 : 1 possibility for sum to be 2
1 + 2 = 2 + 1 = 3 : 2 possibility for sum to be 3
1 + 3 = 2 + 2 = 3 + 1 = 4 : 3 possibility for sum to be 4
.
.
.
6 + 6 = 12 : 1 possibility for sum to be 12
so on average we expect 1/36 of results to be 2, 2/36 of results to be 3, ... and 1/36 results to be 12
the assignment requires you to show that your answers are close to this expected values.
4th program
output
expected[ 0] = 0
expected[ 1] = 0
expected[ 2] = 1
expected[ 3] = 2
expected[ 4] = 3
expected[ 5] = 4
expected[ 6] = 5
expected[ 7] = 6
expected[ 8] = 5
expected[ 9] = 4
expected[10]= 3
expected[11] = 2
expected[12] = 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.