Need help on homework. New to programming.

I need to build a program that gets 50 random numbers in an array and sorts them using selection sort. Here is what i have so far.

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <iomanip>

using namespace std;

const int ARRAY_SIZE = 50;


/*----------------------------------------------------------------------------*/
/* Populate the array with 50 randomly generated integer values
 * in the range 1-50. */
void populateArray(int ar[], const int n);

/* Find the index of the smallest element in array range. */
int indexOfSmallestElement(const int startAt, const int ar[], const int n);

/* Find the index of the largest element in array range. */
int indexOfLargestElement(const int startAt, const int ar[], const int n);

/* Perform a selection sort of the array. */
void selectionSort(int ar[], const int n);

/* Swap the elements indicated by the two indices. */
void swap(int ar[], const int index1, const int index2);

/* Compute the average of all the elements. */
double average(const int ar[], const int n);

/* Compute the sum of all the elements. */
int sum(const int ar[], const int n);

/* Find all the elements that are greater than the threshold and
 * store in the foundElements arrray. Update the count to reflect
 * how many elements were found and stored in the array. */
void findElementsGreaterThan(const double threshold,
                             const int ar[], const int n,
                             int foundElements[], int& count);

/* Display the array. */
void displayArray(const int ar[], const int n);
/*----------------------------------------------------------------------------*/



int main() {
    int ar[ARRAY_SIZE];
    
    /* Populate the array with random numbers. */
    populateArray(ar, ARRAY_SIZE);
    
    /* Display the array. */
    cout << "Original array:" << endl;
    displayArray(ar, ARRAY_SIZE);
    
    /* Sort the array. */
    cout << endl << "Sorted array:" << endl;
    selectionSort(ar, ARRAY_SIZE);
    displayArray(ar, ARRAY_SIZE);
    
    /* Find the smallest value and its index. */
    int index = indexOfSmallestElement(0, ar, ARRAY_SIZE);
    cout << endl
    << "Smallest element: " << ar[index]
    << ", at index [" << index << "]";
    
    /* Find the largest value and its index. */
    index = indexOfLargestElement(0, ar, ARRAY_SIZE);
    cout << endl
    << "Largest element: " << ar[index]
    << ", at index [" << index << "]"
    << endl;
    
    /* Find the sum and average. */
    int sumOfElements = sum(ar, ARRAY_SIZE);
    double ave = average(ar, ARRAY_SIZE);
    
    cout << endl
    << "Sum of elements: " << sumOfElements
    << endl
    << "Average of elements: " << ave
    << endl;
    
    /* Find all the elements largest than the average. */
    int foundElements[ARRAY_SIZE];
    int count;
    
    cout << endl
    << "Elements larger than the average: ";
    findElementsGreaterThan(ave, ar, ARRAY_SIZE, foundElements, count);
    cout << endl;
    displayArray(foundElements, count);
    
    cout << endl << endl;
    
    return 0;
}// end main()
/*----------------------------------------------------------------------------*/

/* Populate the array with 50 randomly generated integer values
 * in the range 1-50. */
void populateArray(int ar[], const int n){
    int randomNumber;
    
    for (int a = 0; a <= ARRAY_SIZE; a++) {
        randomNumber = rand() % 50 + 1;
        
        ar[a]=randomNumber;
        
    }
    


}

/* Find the index of the smallest element in array range. */
int indexOfSmallestElement(const int startAt, const int ar[], const int n){
    int minIndex = startAt;
    
    
    for (int i = startAt; i <= n - 1; i++) {
        minIndex = (ar[i] < ar[minIndex])? i : minIndex;
    }
    
    
    
    return minIndex;
    
    
    
    }

/* Find the index of the largest element in array range. */
int indexOfLargestElement(const int startAt, const int ar[], const int n){
   
    int maxIndex = startAt;
    
    for (int i = startAt; i <= n - 1; i++) {
        maxIndex = (ar[i] > ar[maxIndex])? i : maxIndex;
    }
    
    
    
    return maxIndex;
    
}

/* Perform a selection sort of the array. */
void selectionSort(int ar[], const int n){
    for (int top = 0; top <= n - 2; top++) {
        swap(ar, top, indexOfSmallestElement(top, ar, n));
    }

}

/* Swap the elements indicated by the two indices. */
void swap(int ar[], const int index1, const int index2){
    int tmp = ar[index1];
    ar[index1] = ar[index2];
    ar[index2] = tmp;
}

    
    
    


/* Compute the average of all the elements. */
double average(const int ar[], const int n){
    
    int sum;
    
    double ave = sum /(double) n;
    
    return ave;
    
    
}

/* Compute the sum of all the elements. */
int sum(const int ar[], const int n){
    int sum = 0.0;
    for (int count = 0; count <= n - 1; count++) {
        sum += ar[count];
    
    

    }
    return sum;
}

/* Find all the elements that are greater than the threshold and
 * store in the foundElements arrray. Update the count to reflect
 * how many elements were found and stored in the array. */

void findElementsGreaterThan(const double threshold,const int ar[], const int n,int foundElements[], int& count){
    
    cout<< "1"<<endl;
}




/* Display the array. */
void displayArray(const int ar[], const int n){
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        if((i+1) % 10 == 0) { cout << endl; }
        
        cout << ar[i] << ' ';
        
    }
}








Here is what the output needs to look like.


Original array:
  1   1  31  49  42  21  31  37   2  44
 28  10  43   7   8  48   3  12  40   4 
 26   6  16   1  37  18  21  10  36   3 
 47  18  29   7  30  41  22  44  29  26 
 42  15   1   8  17  41  26  40  35  10

Sorted array: 
  1   1   1   1   2   3   3   4   6   7 
  7   8   8  10  10  10  12  15  16  17
 18  18  21  21  22  26  26  26  28  29
 29  30  31  31  35  36  37  37  40  40
 41  41  42  42  43  44  44  47  48  49

Smallest element: 1, at index [0]
Largest element: 49, at index [49]

Sum of elements: 1164
Average of elements: 23.28

Elements larger than the average:
 26  26  26  28  29  29  30  31  31  35
 36  37  37  40  40  41  41  42  42  43
 44  44  47  48  49

Program ended with exit code: 0





Last edited on
use the [ code ] tags.
One thing I see is that you should do:
1
2
for(int a=0;a<50;a++)
    ar[a] = rand() % 50 + 1;

This makes you go through the array in order from [0] -> [49], giving each one a random number.
Last edited on
You don't need for loop at line 206, since why would you want to print same value of array 10 times?
Instead you should check int i at for(int i = 0; i < ARRAY_SIZE; i++) to put newline after every 10 elements,
like if((i+1) % 10 == 0) { cout << endl }
and also replace line 208 cout << ar[i] << endl; with cout << ar[i] << ' ';

And on 104
1
2
3
4
5
6
    for (int a = 1; a <= ARRAY_SIZE; a++) { // This
        randomNUmber = rand() % 50 + 1;
        
        ar[randomNUmber]++; // and this line
        
    }

You need to change int a = 1 to int a = 0 like Pured said earlier and you need to change ar[randomNUmber]++; to ar[a] = randomNUmber; to assign the number to array.

At line 143 you are returning value 0 instead of the maxValue.

To get your average calculating work, you need to pass sum of the values instead of the array
and then just in your double average function return sum/values;

I did all that stuff and it looks more cleaner and better, but it still isn't correct. I ran the debugger and it is only assigning numbers from 4-50. Also how would I do the find elements greater than function?

My output now looks like-



Original array:
8 50 24 9 31 23 45 29 24 10 41 16 43 43 38 4 28 30 41 
13 4 20 10 8 11 34 50 29 17 36 48 27 13 18 11 34 30 50 
30 22 18 23 44 37 36 46 29 42 45 8 
Sorted array:
4 4 8 8 8 9 10 10 11 11 13 13 16 17 18 18 20 22 23 23 
24 24 27 28 29 29 29 30 30 30 31 34 34 36 36 37 38 41 
41 42 43 43 44 45 45 46 48 50 50 50 

Smallest element: 4, at index [0]
Largest element: 50, at index [47]

Sum of elements: 1380
Average of elements: 27.6

Elements larger than the average: 1

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 


Last edited on
Topic archived. No new replies allowed.