So I'm making a selection sort program where I have to input two values: one for the numbers to use in the array, and the seed to use for the random number generator. I'm just a little confused on how to adjust the number used, since the maximum number of elements we can put in is 15. The array currently has 8. Each element should be a number between 20 and 40.
#include <iostream>
using namespace std;
int selectionSort(int[], int);
int selectionSort(int numbers[], int numbersSize) {
int i = 0;
int j = 0;
int indexSmallest = 0;
int temp = 0; // Temporary variable for swap
for (i = 0; i < numbersSize - 1; ++i) {
// Find index of smallest remaining element
indexSmallest = i;
for (j = i + 1; j < numbersSize; ++j) {
if (numbers[j] < numbers[indexSmallest]) {
indexSmallest = j;
}
}
// Swap numbers[i] and numbers[indexSmallest]
temp = numbers[i];
numbers[i] = numbers[indexSmallest];
numbers[indexSmallest] = temp;
}
return indexSmallest;
}
int main() {
int numbers[] = {10, 2, 78, 4, 45, 32, 7, 11, 0, 0, 0, 0, 0, 0, 0};
const int NUMBERS_SIZE = 15;
int i = 0;
int nums = 0;
int seed = 0;
cin >> nums;
cin >> seed;
srand(seed);
for (int i = 0; i < nums; i++) {
numbers[i] = (rand() % 20) + 20;
}
cout << "UNSORTED: ";
for (i = 0; i < nums; ++i) {
cout << numbers[i] << " ";
}
cout << endl;
selectionSort(numbers, nums);
cout << "SORTED: ";
for (i = 0; i < nums; ++i) {
cout << numbers[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
The only problem is that the random numbers being generated are the wrong numbers. For example, when I input "10 100" (10 for the numbers being used in the array and 100 for the seed), it outputs
UNSORTED: 25 36 35 24 24 34 38 22 29 29
SORTED: 22 24 24 25 29 29 34 35 36 38
when it should be outputting
UNSORTED: 28 28 34 37 36 27 33 36 36 39
SORTED: 27 28 28 33 34 36 36 36 37 39
I'm not really sure how to fix this.