When I allocate the correct number of arrays, the program goes all weird. But when I allocate one extra spot, the program works correctly. However, I'm not sure why.
The purpose of the code is to arrange the numbers in numerical order.
For example, when I write int array[11], which is the correct number of arrays, the program goes weird. But when I I int write array[12], it works correctly. Any suggestions?
Yea, good point about the for loop. Thank you for pointing that out.
Also, if I run the program with array[11], my program runs incorrectly. However if I run the program with array[12], it works fine. I know I allocate one extra spot more than I need. But if I don't do that, programs goes all weird.
@jhkim3 - Were the for loops using < 12 when the array size was 11, or did you change all the 11s to 12s at the same time? (As written in your opening post, with array[12] and i < 12, everything looks fine.)
If you were using < 12 with an array of size 11, then you would be doing as Softrix pointed out: going (one) past the end of the array.
And if you were running your app on Windows then, due to the way local variables are placed on the stack, this extra element would have been where the variable biggest_int is located. So setting it (array[12]) would actually be setting the value of biggest_index.
Yes, I was going past one at the end of the array. However, my problem still remains, I think. So currently there are 11 elements in the my array[12]. If I wanted to allocate the exact amount of memory, I would set my array to array[11]. However, when I set my array to array[11], my program does not run properly. Yet, when I allocate for one extra room in my array, my code runs correctly. Any thoughts?
You're not handling biggest_index quite right!? (Have not looked for a solution...)
Output on my Windows PC (using MinGW GCC)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (13)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (13)
1111
26
29580436
922
421
63
54
14
7
343
8
Output of C++ Shell
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
1111
922
421
343
63
54
26
14
10
8
7