Wrong output?

I know the numbers are randomly generated, but my output is bad. It outputs too many zeros in wait time, or negative numbers, or huge numbers. I can't figure out why. Can someone explain the problem to me?

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <cstdlib>
#include <iostream>
#include <array>
#include <ctime>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

void second()
{
    
    array<int, 50> service1;
    array<int, 50> service2;
    array<int, 50> priority1; //Creates a queue priority to hold priority number
    array<int, 50> priority2;
    array<int, 100> waithigh;
    array<int, 100> waitmed;
    array<int, 100> waitlow;
    
    int i;
    int random1;
    int random2;
    int high;
    int med; 
    int low; 
    int sum; 
    int averageh;
    int averagem;
    int averagel;
    
    for(i=0; i<50; i++) //Service and wait time
    {
        service1[i] = rand() % 4 + 5;
        service2[i] = rand() % 4 + 5;
    }
    
    for(i=0; i<50; i++)
    {
        random1 = rand() % 3 + 1;
        random2 = rand() % 3 + 1;
        priority1[i] = random1;
        priority2[i] = random2;
        switch(random1)
        {
            case 1:
                low++;
                waitlow[i] = service1[i];
                 break;
            case 2:
                med++;
                waitmed[i] = service1[i];
                break;
            case 3:
                high++;
                waithigh[i] = service1[i];
                break;
        }
        switch(random2)
        {
            case 1:
                low++;
                waitlow[i] = service2[i];
                 break;
            case 2:
                med++;
                waitmed[i] = service2[i];
                break;
            case 3:
                high++;
                waithigh[i] = service2[i];
                break;
        }
    }
    for(i=0; i<low; i++)
    {
        sum += waitlow[i];
    }
    averagel = sum/100;
    
    for(i=0; i<med; i++)
    {
        sum += waitmed[i];
    }
    averagem = sum/100;
    
    for(i=0; i<high; i++)
    {
        sum += waithigh[i];
    }
    averageh = sum/100;
    
    cout << "Two desks" << endl;
    cout << "Low: " << low << endl;
    for(i=0; i<low; i++)
    {
        cout << "Wait time: " << waitlow[i] << endl;
    }
    cout << "Low average: " << averagel << endl;
    cout << "Med: " << med << endl;
    for(i=0; i<med; i++)
    {
        cout << "Wait time: " << waitmed[i] << endl;
    }
    cout << "Med average: " << averagem << endl;
    cout << "High: " << high << endl;
    for(i=0; i<high; i++)
    {
        cout << "Wait time: " << waithigh[i] << endl;
    }
    cout << "High average: " << averageh << endl;
}

int main() 
{
    srand(time(NULL)); //Initializes srand for random number generator
    array<int, 100> service;
    array<int, 100> priority; //Creates a queue priority to hold priority number
    array<int, 100> waith;
    array<int, 100> waitm;
    array<int, 100> waitl;
    
    int i;
    int random;
    int high;
    int med; 
    int low; 
    int sum; 
    int averageh;
    int averagem;
    int averagel;
    
    for(i=0; i<100; i++) //Service and wait time
    {
        service[i] = rand() % 4 + 5;
    }
    
    for(i=0; i<100; i++)
    {
        random = rand() % 3 + 1;
        priority[i] = random;
        switch(random)
        {
            case 1:
                low++;
                waitl[i] = service[i];
                 break;
            case 2:
                med++;
                waitm[i] = service[i];
                break;
            case 3:
                high++;
                waith[i] = service[i];
                break;
        }    
    }
    for(i=0; i<low; i++)
    {
        sum += waitl[i];
    }
    averagel = sum/100;
    
    for(i=0; i<med; i++)
    {
        sum += waitm[i];
    }
    averagem = sum/100;
    
    for(i=0; i<high; i++)
    {
        sum += waith[i];
    }
    averageh = sum/100;
    
    cout << "Low: " << low << endl;
    for(i=0; i<low; i++)
    {
        cout << "Wait time: " << waitl[i] << endl;
    }
    cout << "Low average: " << averagel << endl;
    cout << "Med: " << med << endl;
    for(i=0; i<med; i++)
    {
        cout << "Wait time: " << waitm[i] << endl;
    }
    cout << "Med average: " << averagem << endl;
    cout << "High: " << high << endl;
    for(i=0; i<high; i++)
    {
        cout << "Wait time: " << waith[i] << endl;
    }
    cout << "High average: " << averageh << endl;
    
    void second();
}
The problem is that in the loop on line 139 you use i for waitl/waitm/waith. Use the true indexes low/med/high instead an increase them after you set the data.

Further more the said indexes and other variables (like sum) are uninitialized. Thus the values remain random since they are never set to anything usefull (0).
I don't understand by what you mean in that first part. Sorry... Also, I went through and made all of the variables such as sum equal 0.
what I have currently:

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <cstdlib>
#include <iostream>
#include <array>
#include <ctime>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main() 
{
    srand(time(NULL)); //Initializes srand for random number generator
    array<int, 100> service;
    array<int, 100> priority;
    array<int, 100> waith;
    array<int, 100> waitm;
    array<int, 100> waitl;
    array<int, 100> serviceh;
    array<int, 100> servicem;
    array<int, 100> servicel;
    
    int i=0;
    int random=0;
    int high=0;
    int med=0; 
    int low=0; 
    int sum=0; 
    int averageh=0;
    int averagem=0;
    int averagel=0;
    
    for(i=0; i<100; i++) //Service and wait time
    {
        service[i] = rand() % 4 + 5;
    }
    
    for(i=0; i<100; i++)
    {
        random = rand() % 3 + 1;
        priority[i] = random;
        switch(random)
        {
            case 1:
                waitl[i] = service[i];
                servicel[i] = service[i];
                low++;
                 break;
            case 2:
                waitm[i] = service[i];
                servicem[i] = service[i];
                med++;
                break;
            case 3:
                waith[i] = service[i];
                serviceh[i] = service[i];
                high++;
                break;
        }    
    }
    for(i=0; i<low; i++)
    {
        sum += waitl[i];
    }
    averagel = sum/100;
    
    for(i=0; i<med; i++)
    {
        sum += waitm[i];
    }
    averagem = sum/100;
    
    for(i=0; i<high; i++)
    {
        sum += waith[i];
    }
    averageh = sum/100;
    
    cout << "Low: " << low << endl;
    for(i=0; i<low; i++)
    {
        cout << "Wait time: " << waitl[i] << endl;
    }
    cout << "Low average: " << averagel << endl;
    cout << "Med: " << med << endl;
    for(i=0; i<med; i++)
    {
        cout << "Wait time: " << waitm[i] << endl;
    }
    cout << "Med average: " << averagem << endl;
    cout << "High: " << high << endl;
    for(i=0; i<high; i++)
    {
        cout << "Wait time: " << waith[i] << endl;
    }
    cout << "High average: " << averageh << endl;
    
    array<int, 50> service1;
    array<int, 50> service2;
    array<int, 50> priority1;
    array<int, 50> priority2;
    array<int, 100> waithigh;
    array<int, 100> waitmed;
    array<int, 100> waitlow;
    
    i = 0;
    int random1=0;
    int random2=0;
    high = 0;
    med = 0; 
    low = 0; 
    sum = 0; 
    averageh = 0;
    averagem = 0;
    averagel = 0;
    
    for(i=0; i<50; i++) //Service and wait time
    {
        service1[i] = rand() % 4 + 5;
        service2[i] = rand() % 4 + 5;
    }
    
    for(i=0; i<50; i++)
    {
        random1 = rand() % 3 + 1;
        random2 = rand() % 3 + 1;
        priority1[i] = random1;
        priority2[i] = random2;
        switch(random1)
        {
            case 1:
                low++;
                waitlow[i] = service1[i];
                 break;
            case 2:
                med++;
                waitmed[i] = service1[i];
                break;
            case 3:
                high++;
                waithigh[i] = service1[i];
                break;
        }
        switch(random2)
        {
            case 1:
                low++;
                waitlow[i] = service2[i];
                 break;
            case 2:
                med++;
                waitmed[i] = service2[i];
                break;
            case 3:
                high++;
                waithigh[i] = service2[i];
                break;
        }
    }
    for(i=0; i<low; i++)
    {
        sum += waitlow[i];
    }
    averagel = sum/100;
    
    for(i=0; i<med; i++)
    {
        sum += waitmed[i];
    }
    averagem = sum/100;
    
    for(i=0; i<high; i++)
    {
        sum += waithigh[i];
    }
    averageh = sum/100;
    
    cout << "Two desks" << endl;
    cout << "Low: " << low << endl;
    for(i=0; i<low; i++)
    {
        cout << "Wait time: " << waitlow[i] << endl;
    }
    cout << "Low average: " << averagel << endl;
    cout << "Med: " << med << endl;
    for(i=0; i<med; i++)
    {
        cout << "Wait time: " << waitmed[i] << endl;
    }
    cout << "Med average: " << averagem << endl;
    cout << "High: " << high << endl;
    for(i=0; i<high; i++)
    {
        cout << "Wait time: " << waithigh[i] << endl;
    }
    cout << "High average: " << averageh << endl;
}



Now, when it outputs, the only outputs that I have a problem with (as they're outputting as large, negative numbers) is the high wait time outputs. I ran through it a few times and low and medium were fine each time, high was the only one that had a problem. This is the only place in my code that I feel like could mess it up, but I do not understand since my 2nd bit of code that has almost the same thing works fine, and low and medium work fine and they're coded the same as high is in this loop...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    for(i=0; i<100; i++)
    {
        random = rand() % 3 + 1;
        priority[i] = random;
        switch(random)
        {
            case 1:
                waitl[i] = service[i];
                servicel[i] = service[i];
                low++;
                 break;
            case 2:
                waitm[i] = service[i];
                servicem[i] = service[i];
                med++;
                break;
            case 3:
                waith[i] = service[i];
                serviceh[i] = service[i];
                high++;
                break;
        }    
    }
Last edited on
Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        switch(...)
        {
            case 1:// Note: low is the index of waitlow not i
                waitlow[low] = service1[i];
                low++;
                 break;
            case 2:// Like low
                waitmed[med] = service1[i];
                med++;
                break;
            case 3:// Like low
                waithigh[high] = service1[i];
                high++;
                break;
        }
Topic archived. No new replies allowed.