Dear users,
I have just registered with this forum, and introduced myself as such:
My question regards a 2d array, to be sorted according to the first row, such that an array such as
{4,6,3,7,2},{1, 2,3,4,5} would be sorted to
{2,3,4,6,7},{5, 3,1,2,4}
I have taken the following example code from an FAQ on the internet, but cannot make it work as intended.
which returns
Any assistance appreciated!
Andy
I have just registered with this forum, and introduced myself as such:
My question regards a 2d array, to be sorted according to the first row, such that an array such as
{4,6,3,7,2},{1, 2,3,4,5} would be sorted to
{2,3,4,6,7},{5, 3,1,2,4}
I have taken the following example code from an FAQ on the internet, but cannot make it work as intended.
Code:
#include <stdio.h>
#include <stdlib.h>
int values[2][6] = {
{4, 1, 10, 9, 2, 5 },
{1, 2, 3, 4, 5, 6}
};
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int i;
qsort (values, 6, sizeof(int), compare);
for (i = 0; i < 6; i++)
{
printf ("%d ",values[0][ i ]);
}
printf("\n");
for (i = 0; i < 6; i++)
{
printf("%d " , values[1][i]);
}
return 0;
}
Code:
1 2 4 5 9 10 1 2 3 4 5 6
Andy
Comment