Question #652

I managed to create a random number generator, which generates 100 numbers from -20 to 20.
But now i need to rearrange them in a way that the positive numbers will be presented first, then the zeros, and then the negative numbers.
Step 1 is to generate the random numbers (which I managed to do) and step 2 is to press any key (ReadKey) and then the numbers will be rearranged in the way I just wrote about. How do i that?

Expert's answer

First of all, fill the array [0..99] with the generated random numbers.

Then use ReadKey to assume that key was pressed.

After the key has been pressed, use "bubble sorting", the pseudocode is following:

int temp;

for (int i = 0; i < 99; i++) {

for (int i = 0; i < 99; i++) {

if (your_array[i] < your_array[i + 1]) {

temp = your_array[i];

your_array[i] = your_array[i + 1];

your_array[i + 1] = temp;

}

}

}

After the sorting, just output your array, and the positive numbers will be presented first, then the zeros, and then the negative numbers.

Then use ReadKey to assume that key was pressed.

After the key has been pressed, use "bubble sorting", the pseudocode is following:

int temp;

for (int i = 0; i < 99; i++) {

for (int i = 0; i < 99; i++) {

if (your_array[i] < your_array[i + 1]) {

temp = your_array[i];

your_array[i] = your_array[i + 1];

your_array[i + 1] = temp;

}

}

}

After the sorting, just output your array, and the positive numbers will be presented first, then the zeros, and then the negative numbers.

## Comments

## Leave a comment