qsort with "compare" function in an 2D array

From: bsder (bsder_at_bsder.com)
Date: 07/14/05


Date: Thu, 14 Jul 2005 14:18:20 GMT

Hi,

I finally written a full program with comparison in a 2-dimensional
array. But the compilation is not successful. It appears that I didn't
pass in the correct method "compare".
Can anyone please guide me how to correct the error?

Here is the program:

#include <stdlib.h>
#include <math.h>

int compare(const double *rowA, const double *rowB)
{
     double diffA[3], diffB[3], distance[2];
     diffA[0] = 100.0 - *rowA+1;
     diffA[1] = 100.0 - *rowA+2;
     diffA[2] = 100.0 - *rowA+3;

     diffA[0] = 100.0 - *rowB+1;
     diffA[1] = 100.0 - *rowB+2;
     diffA[2] = 100.0 - *rowB+3;

     distance[0] = sqrt(diffA[0]+diffA[1]+diffA[2]);
     distance[1] = sqrt(diffB[0]+diffB[1]+diffB[2]);

     return 0 ? distance[0] > distance[1] : 1;
}

void prn_sorted_distance(double spc[][4], int n)
{
     //qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
printf("size_spc*: %d\n", sizeof *spc);
     qsort(spc, n, sizeof *spc, compare);
     prn_distance(spb, n);
}

void prn_distance(double spb[][4], int n)
{
     register int x,y,z, t,coor=0;
     double distance;
     int size_coor = sizeof spb[0] / sizeof spb[0][0];
     double tmp[size_coor];
     if (n == 0 || size_coor < 3)
        return;

     for ( t = 0; t < n; t++ ) {
         for ( coor = 0; coor < size_coor; coor++ ) {
             printf("point: %6.1lf; ", spb[t][coor]);
             tmp[coor] = 100.00-spb[t][coor];
         }
         x = tmp[0] * tmp[0];
         y = tmp[1] * tmp[1];
         z = tmp[2] * tmp[2];
         distance = sqrt(x+y+z);
         printf("distance: %6.1lf\n", distance);
     }
}

int main()
{
     double spb[3][4] = {
           {1.0, 2.0, 1.0, 3.0},
           {8.0, 3.0, 12.0, 8.0},
           {4.0, 7.0, 2.0, 5.0}
     };

     int size_elem = sizeof spb / sizeof spb[0];
     prn_distance(spb, size_elem);
     printf("--------------------------\n");
printf("size_spc: %d\n", sizeof spb/sizeof *spb);
printf("size_spc*: %d\n", sizeof *spb);
     prn_sorted_distance(spb, size_elem);

     return 1;
}

Thanks
D



Relevant Pages

  • Re: qsort with "compare" function in an 2D array
    ... On 2005-07-14, bsder wrote: ... > array. ... But the compilation is not successful. ...
    (comp.unix.bsd.freebsd.misc)
  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Strategy or Iterator?
    ... It would be possible to write a class that returns the variations ... GNU General Public License for more details. ... protected CombinatoricOperator(Telements, int r) { ... An integer array backing up the original one to keep track of the ...
    (comp.lang.java.programmer)
  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: What is a type?
    ... >> compilation time. ... The type int is perfectly supported by ... You probably won't find the term "int" in a CPU reference manual. ... I meant that a function pointer could be implemented as an index into ...
    (comp.lang.c)