The goal for my assignment is to create an array of size 6 which initially is all 0. The next step is to roll a die 6000 times, logging in each number that is rolled. The idea is that for each 0 0 0 0 0 0, the 0 will tick up to 1, 2, 3, 4, etc. for each 1-6 of the die that is rolled. example: if the first roll is 2 3 4 2 5 5 The ticker will count 0 2 1 1 2 0, because there was 2 2's (in the 2 spot), 1 3 (in the 3 spot), 1 4 (in the 4 spot), and 2 5's (in the 5 spot).
A serious issue that I'm encountering so far is segmentation fault for my array_size which seems to be too large. When I shrink it to 2000, it fits. I think this is related to the srand(time(0)) command, which makes random work by setting it to the time using #include.
/ Program: random_die_rolls.cpp
// Author: Jared Y
// Date: 10/5/16
// Simulates the rolling of a die 6000 times and displays in a
// table format the number of occurrences of each side of the die
#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]; // array (num_array)of length array_size
int tally_array[6]; // 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;
//cout << i << " " << num_array[i] << endl;
}
// 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:
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 wil hold the sum of all rolls of 2's, etc..
In order to achieve this, create a loop, cycling through each of the
6,000 random values, where each value (1 through 6) becomes the
index of tally_array and that index element will get
incremented by one with the ++ operator.
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;
/** cout << "Display Result for 6000 Rolls" << endl;
cout << "Die Face Occurance" << endl;
cout << "===========================" << endl;
for (int x=1; x<=6; x++) {
die[x] = 99; // temp data
cout << x << fixed << setw(20) << die[x] << 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;
}
You're not really thinking about what you're doing. You write that you only need 6 slots in the array representing the possible value of a single die, but the subscript you're using to access that array goes up to 6000. I don't know why you called the variable to store the amount of times a die needs to be rolled "array_size" either.
Change
1 2 3 4 5 6 7 8
constint rollCount=6000;
int value;
for(int i=0; i < rollCount; i++)
{
value= rand%6 //0 through 5
tally_array[value]=++tally_array[value];
}
num_array was defined with a size of 6 instead of array_size, Arrays are indexed from 0 to size-1. I corrected a few things that I noticed and added a switch statement to count the tally.
// Program: random_die_rolls.cpp
// Author: Jared Y
// Date: 10/5/16
// Simulates the rolling of a die 6000 times and displays in a
// table format the number of occurrences of each side of the die
#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 num_array[array_size]; // array (num_array)of length array_size
int tally_array[7] = {}; // 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:
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 wil hold the sum of all rolls of 2's, etc..
In order to achieve this, create a loop, cycling through each of the
6,000 random values, where each value (1 through 6) becomes the
index of tally_array and that index element will get
incremented by one with the ++ operator.
Makes sense? If not, let's talk about it on the db
*/
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<7; i++)
{
cout << "i: " << i << " " << tally_array[i] << endl;
}
cout << endl;
/** cout << "Display Result for 6000 Rolls" << endl;
cout << "Die Face Occurance" << endl;
cout << "===========================" << endl;
for (int x=1; x<=6; x++) {
die[x] = 99; // temp data
cout << x << fixed << setw(20) << die[x] << 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
**/
}
Display Result for 6000 Rolls
Die Face Occurance
===========================
1 7
2 3
3 2
4 1
5 1
6 500524290
*** stack smashing detected ***: ./random_die_rolls terminated
Aborted
Does anyone know what's happening?
edit: I also get a segmentation fault when I set array_size to 6000
edit2: I've tried setting the size to 20, but the count overshoots that number by quite some noticeable amount.
#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 = 20; // 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:
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 wil hold the sum of all rolls of 2's, etc..
In order to achieve this, create a loop, cycling through each of the
6,000 random values, where each value (1 through 6) becomes the
index of tally_array and that index element will get
incremented by one with the ++ operator.
Makes sense? If not, let's talk about it on the db
*/
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;
}