Trying to understand tallying an array

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/ 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.
using namespace std;

int main()
{
                // declare variables
        const int 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
const int rollCount=6000;
int value;
for(int i=0; i < rollCount; i++)
{
value= rand%6 //0 through 5
tally_array[value]=++tally_array[value];
}
closed account (LA48b7Xj)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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.
using namespace std;

int main()
{
    // declare variables
    const int 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
    **/
}
when do we use the variable die? I think it's required.
closed account (LA48b7Xj)
You could put the random number into the variable first then save it to the array.
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.
Last edited on
Stack overflow. Post your updated code.

EDIT:lol dude you need to read the answers given to you.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <ctime>                // for the time() function
#include <cstdlib>              // for the srand() and rand() functions
#include <iomanip>              // for formatting, setw() etc.
using namespace std;

int main()
{
                // declare variables
        const int 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;
}

debug for num_array[]
-2119601396 32765 -2119601376 32765 -2119601359 32765 -2119601343 32765 -2119601308 32765 -2119601290 32765 -2119601267 32765 -2119601199 32765 -2119601176 32765 -2119601131 32765 -2119601120 32765 -2119599704 32765 -2119599679 32765 -2119599660 32765 -2119599602 32765 -2119599550 32765 -2119599509 32765 -2119599458 32765 -2119599390 32765 -2119599367 32765 -2119599224 32765 -2119599205 32765 -2119599172 32765 -2119599142 32765 -2119599121 32765 -2119599105 32765 -2119599079 32765 -2119599058 32765 -2119599039 32765 -2119599022 32765 -2119599007 32765 -2119598953 32765 -2119598926 32765 -2119598897 32765 -2119598879 32765 -2119598862 32765 -2119598844 32765 -2119598818 32765 -2119598787 32765 -2119598779 32765 -2119598762 32765 -2119598747 32765 -2119598729 32765 -2119598685 32765 -2119598667 32765 -2119598635 32765 -2119598608 32765 -2119598594 32765 -2119598568 32765 -2119598508 32765 
debug for tally_array
i: 1 6
i: 2 5
i: 3 6
i: 4 7
i: 5 11
i: 6 8

Display Result for 6000 Rolls
Die Face    Occurences
===========================
1                  6
2                  5
3                  6
4                  7
5                 11
6                  8
*** stack smashing detected ***: ./random_die_rolls terminated
Aborted

I changed the num_array to the array_size length and it's working, thanks for the help. I really appreciate it!
Topic archived. No new replies allowed.