Write a C++ program that simulates the rolling of two six sided die. The program
ID: 3663392 • Letter: W
Question
Write a C++ program that simulates the rolling of two six sided die. The program should call a function called roll that returns the value of the die. This function should use the pseudorandom number generator function, rand(). The sum of the two values should then be calculated. Since each die is six-sided, the sum should range in values from 2 to 12, with 7 being the most frequent and 2 and 12 the least frequent. Note: you will need to generate two random numbers from 1 to 6 to represent the two dies. Your program should roll the two die 36,000 times. Use a two dimensional array to tally the number of times each possible sum appears and print the results in tabular format.
Explanation / Answer
#include<iostream>
#include<ctime>
using namespace std;
int getRepetitions();
int rollDie(int);
void doRolling(int[], int, int);
void showSummary(int[], int, int);
double i;
int main()
{
int uInput;
uInput = getReptitions();
rollDie(uInput);
doRolling();
showSummary();
system("pause");
return 0;
}
int getRepetitions()
{
int userInput = 1;
while(userInput > 0)
{
cout<"How many times do you want to roll the dice? "<<endl;
cin>>userInput;
}
return userInput;
}
int rollDie(int userInput)
{
int counts[Max_number_of_rolls] = {0};
srand(time(NULL));
int dice;
dice = 1+ rand()%6;
return dice;
}
void doRolling(int rollTimes[], int rollsRequired, int NumberOfSides)
{
cout << "----------------Rolls of the first and second dice--------------" << endl;
for(int i=0; i<rollDie; i++)
{
srand(time(NULL));
int dice;
dice= 1+ rand()%6;
counts[i]=dice;
cout << "Roll # " << i+1 << " first dice is: " << dice << endl;
dice= 1+ rand()%6;
counts[i]=counts[i]+dice; // First and Second Dice Adds Together
cout << "Roll # " << i+1 << " second dice is: " << dice << endl;
cout << "The sum is: " << counts[i] << endl;
}
cout<< "-------------Results/Statistics----------------" << endl;
// To Calculate the Percentage
for(i=2;i<13;i++)
{
int c =getRepetitions(counts,rollDie,i);
cout << "The total value of " << i <<" is repeated " << c << " out of " << rollDie << " times." << endl;
percentage= ((double)c/(double)rollDie)*100;
cout << "Percentage of the time the total value was rolled is: " << percentage << "%" << endl;
}
cout << endl << endl;
}
void showSummary(int[], int, int)
{
cout<< "-------------Results/Statistics----------------" << endl;
// To Calculate the Percentage
for(i=2;i<13;i++)
{
int c =getRepetitions(counts,rollDie,i);
cout << "The total value of " << i <<" is repeated " << c << " out of " << rollDie << " times." << endl;
percentage= ((double)c/(double)rollDie)*100;
cout << "Percentage of the time the total value was rolled is: " << percentage << "%" << endl;
}
cout << endl << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.