Question: random_die_rolls.cpp : exercise to practice arrays and random generation Write a program that simulates the rolling of a die 6000 times and displays in a
random_die_rolls.cpp: exercise to practice arrays and random generation
Write a program that simulates the rolling of a die 6000 times and displays in a table format the number of occurrences of each side of the die. Note: die (referring to the small cube used in games of chance) is singular for the plural dice.
Start with the following file, containing pseudo-code, which already describes the different steps of the algorithm. Do use the variable name as specified in the pseudo-code:
random_die_rolls_start.txt
code ----
#include#include<> // for the time() function #include<> // for the srand() and rand() functions #include<> // for formatting, setw() etc. using namespace std; int main() { // declare variables const ... // a constant (array_size) declared and assigned to 6000 ... // a variable (die) to represent one random die number ... // array (num_array)of length array_size ... // array (tally_array) to hold the totals of each die face // Note: tally_array needs 7 elements and initialization with 0's // set the seed first, and only once (before creating all random numbers) ... // Populate array_size random numbers into array num_array, in a loop // Test part of the array for correct data // Leave this debug statement in final app cout << "debug for num_array[]" << endl; for (int i=100; i<200; i++) { cout << num_array[i] << " "; } cout << endl; /* Tally the result - this is tricky - understand it fully: First, do not use the switch statement nor if statements. Declare an array, tally_array with 7 elements - only indexes 1-6 (not 0) will be used to hold the total of each die face values - index 1 will hold the sum of all rolls of 1's, index 2 will hold the sum of all rolls of 2's, etc.. Here's pseudocode for the tallying process: Loop through every num_array elements (6000 times) with counter i assign current value of num_array at index i to variable x access tally_array index x and increment end loop Makes sense? If not, let's talk about it on the db */ // Test tally_array for correct data cout << "debug for tally_array" << endl; for (int i=1; i<=6; i++) { cout << "i: " << i << " " << tally_array[i] << endl; } cout << endl; // display the results // ruffly in this format, the main thing being that Occurrences' // are right alined. /* Display Result for 6000 Rolls Die Face Occurrences =========================== 1 1017 2 1033 3 949 4 1026 5 987 6 988 */ return 0; }
code ends///
In order to shed a bit more light on the required method of tallying the 6000 values into tally_array (as described in a comment in the random_die_rolls_start.cpp above), here is an example of the first few iteration of the loop:
At the beginning, the tally_array should contain 7 zeros: 0 0 0 0 0 0 0 and let's say that the first five num_array elements from the die throws contain these values: 2 4 6 2 2 ... In the loop the first element '2' would point to the third element (index 2) in tally_array to access it - then, increment it. Now, tally_array looks like this: 0 0 1 0 0 0 0 By the time the loop runs through the first five elements of the 6000 rolls, tally_array will look like this: 0 0 3 0 1 0 1 Makes sense? You must understand this before going any further.
The start file already contains code to help debug important variable contents. Leave those in for the final output and add to it if necessary. These debug statements are there to encourage the student to adopt similar systems in order to understand what the program is doing and check code.
display the results ruffly in this format, the main thing being that Occurrences' are right alined.
Display Result for 6000 Rolls
Die Face Occurrences ===========================
1 1013
2 972
3 963
4 1018
5 975
6 1059
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
