Writing an application that accepts 10 numbers and displays them in descending order. It almost works but it's outputting one junk number and 9 of my 10 input numbers. What am I missing, please?
I don't understand what you mean. The array does exist and it accepts and sorts 9 of the 10 integers that are input. It's just one number that's wrong.
If he starts his loop at 0, it should never hit the 10th element of the array. I think your problem is that you have a constant int for the size of the array. Since your program doesn't ask the user for the size of the array, why would you want to use a constant integer?
@Inspireftw,
Please READ what @Repeater and @Nuderobmonkey wrote. You have just negated all that they said. YOU are still accessing beyond the end of the array (on your lines 23 and 24, when j = 9).
I think your problem is that you have a constant int for the size of the array.
That is also nonsense. @madams8377 correctly used a compile-time constant, which only needs to be adjusted ONCE if the size is changed. You, on the other hand, have just used the "magic number" 10, which has to be changed consistently on FIVE separate lines should the array size change.
If j = 9 then the if statement says if (myArray[9] < myArray[10])
How do you even propose to evaluate that? It's neither "true" nor "false", it is "undefined" and you will be quite lucky if it doesn't segfault.
and you will be quite lucky if it doesn't segfault.
*unlucky :)
Also... I agree with MikeyBoy, that guy you replied to is a troll...
madams8377,
Looks OK, but note that you should try to keep variables to the narrowest scope in which they are needed.
Your i and j variables can be written in the for loops themselves to reduce their scope. for (int i = 0; i < SIZE; i++)
Your temp variable can be moved to be declared right where it's used.
1 2 3
int temp = NUMBERS[j];
NUMBERS[j] = NUMBERS[j+1];
NUMBERS[j+1] = temp;
Also, ALLCAPS name are usually meant to be macros or, in some styles, constants. Your NUMBERS array is not a constant or macro, so I would avoid using all-caps.