Due tonight, C++ please help. Write a program that simulates the rolling of two
ID: 3682184 • Letter: D
Question
Due tonight, C++ please help.
Write a program that simulates the rolling of two dice. The program should use rand to toll the first die and should use rand again to toll the second die. The sum of the two values should then be calculated Figure 7.26 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 number of time s each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable (i.e., there are six ways to toll a 7, so approximately one-sixth of all the rolls should be 7).Explanation / Answer
int roll_die() {
return (rand() % 6) + 1;
}
void two_dice_addition() {
cout << "This program rolls two dice n times and tallies the number of times each possible sum appears." << endl;
while (true) {
int tally[11] = {0}; // 2's index: 0, 3: 1, 4: 2, 5: 3, 6: 4, 7: 5, 8: 6, 9: 7, 10: 8, 11: 9, 12: 10
double representation, num_of_asterisks = 200;
cout << "How many times do you wish to roll the dice? (Enter 0 to exit the program) ";
unsigned int n;
cin >> n;
while (n < num_of_asterisks) {
if (n == 0) {
return;
}
cout << "Please enter a number larger than or equal to " << num_of_asterisks << endl;
cin >> n;
}
srand(time(0));
for (unsigned int iterations = 0; iterations <= n; iterations++) {
int sum = roll_die() + roll_die();
tally[sum - 2]++;
}
representation = (1.0 / num_of_asterisks) * n;
cout << "SUM" << setw(10) << "TALLY" << setw(10) << "GRAPH" << endl << setw(2);
for (int tally_index = 0; tally_index < 11; tally_index++) {
cout << tally_index + 2 << setw(10) << tally[tally_index] << setw(10);
for (int asterisk = 1; asterisk <= (tally[tally_index] / representation); asterisk++) {
cout << "*";
}
cout << setw(2) << endl;
}
cout << "Each asterisk represents approx. " << representation << " tallies." << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.