Hi guys.
I am a beginner and I have a problem with my assignment.
I tried to write a code on my own. I was able to create an array on length n (with random numbers), but when I tried to sort it, it does not work and I do not know why. Can somebody help me, please?
I understand the Polish language (maybe 95%), but I cannot write without mistakes, so I decided to ask in English. So you can answer in Polish. Thanks a lot!
#include <iostream>
#include <cstdlib>
#include <ctime> // define "time" in 32th row
#include <cassert>
#include <algorithm>
using namespace std;
int main() {
// CREATE AN ARRAY OF LENGTH "n" AND FILL IT WITH RANDOM NUMBERS
int n = 0; // size of an array at the beginning
int i = 0; // for cycle
int j = 0;
int random_number = 0; // random number at the beginning
int* array = nullptr;
cout << "Please enter a number: "; // write the message for an user
cin >> n; // this allow to change n (in command line)
array = new int[n]; // change n and save it to "array"
srand((unsigned)time(0));
for (i = 0; i < n; i++) { // for cycle from i=0 to i=(n-1)
random_number = (rand() % n); /* generate a random number from 0 to n (I want to avoid the situation,
when somebody write n too hight and I set up too small random n (there would be a lot of the same numbers)*/
array[i] = random_number; // it fills array with random numbers
cout << array[i] << ' ';
if ((i + 1) % 10 == 0) { // write 10 numbers in one row (only for clarity)
cout << '\n';
}
}
return 0;
// SORTING
sort(array, array + n);
for (j = 0; j < n; j++) {
//cout << "Sorted array is:" << endl;
cout << array[j] << " ";
}
}