Mail Archives: djgpp/2005/06/17/20:15:35
badri <yatindran AT gmail DOT com> wrote:
> this is my 2d array.
> int a[][6] = {
> {5,2,20,1,30,10},
> {23,15,7,9,11,3},
> {40,50,34,24,14,4},
> {9,10,11,12,13,14},
> {31,4,18,8,27,17},
> {44,32,13,19,41,19},
> {1,2,3,4,5,6},
> {80,37,47,18,21,9}
> };
> after
> for(i=0;i<box;i++)
> qsort(a[i],dim,sizeof(int),dim_sort);
> int dim_sort(const void *a,const void *b)
> {
> return ( *(int*)a - *(int*)b);
> }
You really should indent your source appropriately. It eases reading
and debugging it.
> where dim=6, i get the foll o/p:
> 1 2 5 10 20 30
> 3 7 9 11 15 23
> 4 14 24 34 40 50
> 9 10 11 12 13 14
> 4 8 17 18 27 31
> 13 19 19 32 41 44
> 1 2 3 4 5 6
> 9 18 21 37 47 80
> (works fine!)
> now i want to sort each row of elements for above array,
> like
> 1 2 3 4 5 6
> 1 2 5 10 20 30
> ...
> etc.
You might want to improve your English writing (or discover the
shift key).
Some rules:
Each setence starts with a capital letter.
I is always spelled "I".
"foll" isn't an English word.
> what is the qsort routine and function, can anybody help me ?
It's the same qsort(). There's only one (unless you make your own and
intrude on the ANSI C namespace, which I don't recommend).
For the comparision function that you pass to qsort(), consider that
if the first array elements are equal (if they aren't you're done and
can return an appropriate return value), you need to check the next
array elements.
Repeat as necessary.
Also consider how you know that you've reached the arrays' last
elements.
Right,
MartinS
- Raw text -