I'm having trouble trying to figure out how to get the program to print of groups of 10 across for both the sorted and unsorted.
The original problem:
Generate 50 random numbers between 1 and 10000, display them in groups of 10 across, sort them, and again display them in groups of 10 across.
UNSORTED:
42 8469 6335 6503 9171 5726 1480 9361 6965 4467
5706 8148 3284 6829 9962 492 2996 1944 4828 5437
2395 4606 3903 154 293 2384 7423 8718 9720 9897
5448 1729 4773 1540 1870 9914 5670 6302 7037 9895
8706 3814 1326 337 7675 4665 5143 7712 8256 6869
SORTED:
42 154 293 337 492 1326 1480 1540 1729 1870
1944 2384 2395 2996 3284 3814 3903 4467 4606 4665
4773 4828 5143 5437 5448 5670 5706 5726 6302 6335
6503 6829 6869 6965 7037 7423 7675 7712 8148 8256
8469 8706 8718 9171 9361 9720 9895 9897 9914 9962
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
|
#include <iostream>
#include <random>
#include <ctime>
#include <cstdlib>
using namespace std;
void 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; ++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;
}
}
int main() {
//Random number
srand((unsigned int)time(NULL));
const int numbersSize = 50;
int numbers[numbersSize];
int i = 0;
cout << "UNSORTED: ";
for (i = 0; i < numbersSize; ++i) {
numbers[i] = rand() % 10000 + 1;
cout << numbers[i] << ' ';
}
cout << endl;
SelectionSort(numbers, numbersSize);
cout << "SORTED: ";
for (i = 0; i < numbersSize; ++i) {
cout << numbers[i] << ' ';
}
cout << endl;
return 0;
}
| |