Some bug with array program

This program that I wrote works fine expect that it outputs the number 1606416376. So I guess that there's a bug somewhere in the program.

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
#include <iostream>
#include <iomanip>
#include <ctime>


using namespace std;

int main()
{
    const size_t SIZE = 600;
    const int RANGE = 100;
    int x[SIZE];
    int num;
    int count[RANGE] = { 0 };
    int i, r;
    
    
    num=SIZE;
    
    srand(time(0));
    for( i = 0; i < num; i++ )
    {
        r = rand() % RANGE + 1;
        x[i] = r;
        count[r]++;
    }

    cout << "\n\nThe random numbers are:\n";
    for(i=0; i<=num; i++)
    {
        cout << setw(5) << x[i];
        if((i+1)%10 == 0) 
            cout << endl;
    }
    
 
    int sum = 0;
    float average = 0;
    for(size_t i=0; i < num; i++)
    {
        sum += x[i];
    }
    average = static_cast<float>(sum) / num;
    cout << endl << "Average = " << average << endl << endl;
    
    int min;
    int max;
    
    min=x[0];
    max=x[0];
    for(int i=1;i<num;i++)
    {
        if(min>x[i])
        {
            min=x[i];
        }
        else if(max<x[i])
        {
            max = x[i];
        }
    }
    
    cout<<"Maximum number is: "<< max << endl;
    cout<<"Minimum number is: "<< min << endl << endl;
    

    int element=0;
    int count2=0;
    
    for(int i=0;i<num;i++)
    {
        int tempElement=x[i];
        int tempCount=0;
        for(int i=0;i<num;i++)
            if(x[i]==tempElement)
                tempCount++;
        if(tempCount>count2)
        {
            element = tempElement;
            count2 = tempCount;
        }
        
    }
    cout << "The most ocurring number is: " << element << "     Frequent: " << count2 << endl << endl;
        
    
    int searchNumber;
    cout << "Enter a search number: ";
    cin >> searchNumber;
    size_t foundIdx = -1;
    for (auto i = 0; i < num; i++)
    {
        if (x[i] == searchNumber)
        {
            if(foundIdx == -1) cout << "Index for the found number is:";
            foundIdx = i;
            cout << ", " << foundIdx;
        }
    }
    
    cout << "\n";
 
    if(foundIdx == -1)
    {
        cout << "The number is not in the array!" << endl;
    }
    
    
    int j;
    int test=num-1;
    
    for(i=0; i<=test; i++)
    {
        for(j=i+1; j<=num; j++)
        {
            int temp;
            
            if(x[i] > x[j])
            {
                temp=x[i];
                x[i]=x[j];
                x[j]=temp;
            
            }
        }
        
    }
    
    cout << "\n\nThe random numbers in order are:\n";
    for(i=0; i<=num; i++)
    {
        cout << setw(5) << x[i];
        if((i+1)%10 == 0) 
            cout << endl;
    }
    
    cout << "\n\nThe random numbers in order by row are:";
    for ( j = 0; j < RANGE; j++ )
    {
        for ( i = 0; i < count[j]; i++ ) cout << j << " ";
        cout << endl;
    }
    
    return 0;
    
}
Last edited on
The problem is that you sometimes use <= num which is wrong (out of bounds) and somethimes < num which is correct.
@coder777 thanks alot!
Topic archived. No new replies allowed.