-
Template Sorting Problem
I've looked over my code several times and I'm unable to find out what I did wrong. It looks ok to me. Its suppose to set all of the elements of the array to random numbers under 100 and then sort those numbers. Instead of outputting numbers under 100, it outputs weird numbers. See for yourself.
-
When using
Code:
MAX= sizeof(array) / sizeof(array[0]);
MAX is assigned a value of 1. This is because sizeof(array) returns the size of the pointer to the array (4 bytes) and sizeof(array[0]) returns the size of an integer (4 bytes). You should pass the number of elements as an extra parameter to the SetArray and SortArray functions.
When outputting, you output all 20 elements of the array, but only the first one was initialized for the reason mentioned above.
For your sorting you should use
Code:
for(int i=0; i<MAX-1; i++)
{
for(int j=i + 1; j<MAX; j++)
and not
Code:
for(int i=0; i<=MAX; i++)
{
for(int j=i + 1; j<=MAX; j++)
In main() you should also reset i to 0 before displaying the sorted array.