#include <iostream>
#include <ctime> // for the time() function
#include <cstdlib> // for the srand() and rand() functions
#include <iomanip> // for formatting, setw() etc.
usingnamespace std;
int main()
{
// declare variables
constint array_size = 6000; // a constant (array_size) declared and assigned to 6000
int die = 0; // a variable (die) to represent one random die number
int num_array[6] = {0}; // array (num_array)of length array_size
int tally_array[6] = {0}; // array (tally_array) to hold the totals of each die face
// set the seed first, and only once (before creating all random numbers)
srand((int)time(0));
// Populate array_size random numbers into array num_array
for (int i=0; i<array_size; i++) {
num_array[i] = (rand() %6) + 1;
}
// 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
*/
for (int i=0; i<array_size; i++) {
switch(num_array[i]) {
case 1:
++tally_array[1];
break;
case 2:
++tally_array[2];
break;
case 3:
++tally_array[3];
break;
case 4:
++tally_array[4];
break;
case 5:
++tally_array[5];
break;
case 6:
++tally_array[6];
break;
}
}
// 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;
cout << "Display Result for 6000 Rolls" << endl;
cout << "Die Face Occurences" << endl;
cout << "===========================" << endl;
for (int i=1; i<=6; i++) {
cout << i << fixed << setw(19) << tally_array[i] << endl;
}
// display the results
// duplicate this format:
/* Display Result for 6000 Rolls
Die Face Occurance
===========================
1 1017
2 1033
3 949
4 1026
5 987
6 988
*/
return 0;
}
num_array is supposed to be array_size in size, not 6.
And you are told in the instructions to not use a switch. You don't need it. Just use num_array[i] directly as the index for tally_array. If you want to index tally_array with values 1 to 6 (instead of the usual 0 to 5 for an array with 6 elements) then you should make it of size 7 instead.
Although note that if i > 6, the first example will not try and access elements beyond tally[6], whereas the second will. Which, of course, is easily fixed...