Cannot fix random number tally program

I dont know what else I need to do to make the program work. The code has comments for clarification.
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
104
105
106
107
#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] = {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;
}
Last edited on
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.
How do I tally the result?
This:
1
2
3
4
5
6
switch (num[i]) {
case 1: ++tally[1]; break;
case 2: ++tally[2]; break;
case 3: ++tally[3]; break;
//...
}

Is the same as this
 
++tally[num[i]];

This [...] Is the same as this [...]


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...
Topic archived. No new replies allowed.