qsort with "compare" function in an 2D array
From: bsder (bsder_at_bsder.com)
Date: 07/14/05
- Next message: Matthias Buelow: "Re: portupgrade sucks? yes. yes, it does."
- Previous message: Kris Kennaway: "Re: portupgrade sucks? yes. yes, it does."
- Next in thread: bsder: "Re: qsort with "compare" function in an 2D array"
- Reply: bsder: "Re: qsort with "compare" function in an 2D array"
- Reply: jpd: "Re: qsort with "compare" function in an 2D array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Matthias Buelow: "Re: portupgrade sucks? yes. yes, it does."
- Previous message: Kris Kennaway: "Re: portupgrade sucks? yes. yes, it does."
- Next in thread: bsder: "Re: qsort with "compare" function in an 2D array"
- Reply: bsder: "Re: qsort with "compare" function in an 2D array"
- Reply: jpd: "Re: qsort with "compare" function in an 2D array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|