Sorting Numbers In Descending Order Gone Wrong

Hello everyone!

My name is Emil, I'm a novice programmer, and I started watching educational YouTube-videos about C++-programming four weeks ago, alongside with occasionally doing exercises for enhancing my knowledge. About a day ago I decided to program a program that could sort numbers in both ascending and descending order, but even after hours of work I could not get the sorting in descending order working, only the one in ascending order. I would like to post a picture showing how the program looks, but since there does not seem to exist such an option, I will describe it for you.

If I enter 10 numbers, for example 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10, and then choose "sort in descending order", this is how the program prints it out on the screen.

6889192
10
9
8
7
6
5
4
3
2


The number on the first line seems to be random, because it changes every time I try to do this. I have a hunch that it has something to do with arrays, maybe the computer is trying to print from (as an example) "array[10]" when there are only nine storaging places. And finally we arrive at my questions. Why is it not working? Do you have any suggestions for how it could be fixed? I will try to enter a as narrowed down version of my code as possible below, so that you hopefully easily can spot the problem. Thanks for enduring this long post, I will be vert grateful if you answer my questions. I hope that I have made myself clear enough for you to understand the problem, and that I have not had too many grammatical errors.

Explanation of the code: The "y" in my code is a number entered by the user, describing how many numbers they want to sort. "Answer" stores the value which describes what order you want to sort in. The code for the sorting in ascending order is almost identical to the code below, except for the "<" sign between emil[z] and emil[z+1], which is replaced by a ">" sign.

Sorting algorithm (descending order):
1
2
3
4
5
6
7
8
9
10
for(int x=0;x<y;x++){
               for(int z=0;z<y;z++){
                    v = z;
                    if(emil[z] < emil[z+1]){
                        v = emil[z];
                        emil[z] = emil[z+1];
                        emil[z+1] = v;
                    }
                }
            }


Printing out the numbers:
1
2
3
4
5
6
if(answer == 2){
        cout << "Here are your numbers, sorted in descending order:" << endl;
        for(int x=0;x<y;x++){
            cout << emil[x] << endl;
        }
    }


Thanks a lot everyone!
1
2
3
 for(int z=0;z<y;z++)
//...
emil[z+1])
Out of bound access on last iteration.
I think I understand what you mean, but how do I fix it?
Last edited on
you can use z < y-1.
Or, if you don't required to provide sorting yourself, use std::sort()
Thank you, it solved the problem! I don't really understand why though, but I will look into that later :)
The array emil[x] is 10 integers from 0 - 9. in the for loop where z is less than y is the error. Y is equal to 10 in this case. when the for loop gets to its final iteration of z < y which is 9, it runs through and gets to emil[z+1] which would be emil[10], which doesn't exist in the actual array, so it's reading a garbage value because the array is from 0-9, and not up to 10.
Oh, now I understand. Thank you, I really appreciate your help! :)
Topic archived. No new replies allowed.