Hi,
I finally written a full program with comparison in a 2-dimensional
array. But the compilation is not successful. It appeared I didn't pass
in the correct method "compare".
compare isn't a method, it is a function.
Can anyone please point me to the direction how to correct this error?
Here is the program:
#include <stdlib.h>
#include <math.h>
You need to include said:
int compare(const double *rowA, const double *rowB)
{
qsort() requires a comparison function that takes 2 const void *
arguments, you've defined one that take 2 const double * arguments i.e.
your function does not meet the specification. If you wanted to sort an
array of doubles you might write:
int compare(const void *vA, const void *vB)
{
const double *rowA = vA;
const double *rowB = vB;
But looking below you're not doing that, you're trying to sort an array
of arrays of double. The first argument you pass to qsort() has type
pointer to an array of 4 doubles or double (*)[4] i.e. the elements to be
sorted each have type array of 4 doubles. qsort() passes a pointer to the
element converted to void *, and that is the type you need to convert back
to:
int compare(const void *vA, const void *vB)
{
const double *rowA = *(const double (*)[4])vA;
const double *rowB = *(const double (*)[4])vB;
The cast converts to the correct pointer to array type, this pointer is
then dereferenced to give the element to be compared (an array), that
array is then automatically converted to a pointer to its first element
which is assigned.
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;
Should these be diffB? Do you mean rowA[1] etc.?
distance[0] = sqrt(diffA[0]+diffA[1]+diffA[2]);
distance[1] = sqrt(diffB[0]+diffB[1]+diffB[2]);
Should those be sums of squares? Also sqrt() is pointless here because it
won't change the relative ordering of the 2 values except for limits of
accuracy issues.
return 0 ? distance[0] > distance[1] : 1;
Did you mean
return distance[0] > distance[1] ? 0 : 1;
This isn't a valid comparison function. A valid comparison function must
produce a consistent total ordering of the array elements. For example if
X > Y then Y < X. Since this function never returns a value < 0 that is
clearly not the case for this function.
It would be a lot safer (and quite probably faster) to generate a separate
array of "distance" values, or square of distance values, and then sort on
that.
}
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);
the result of sizeof has type size_t, not int, so it is not valid to use a
%d conversion specifier for it. A simple fix is:
printf("size_spc*: %d\n", (int)sizeof *spc);
qsort(spc, n, sizeof *spc, compare);
Correct, your commented out version is invalid because spc isn't an array,
it is a pointer. You cannot pass arrays as function arguments in C, the
true type of spc is double (*)[4] i.e. a pointer to an array of 4 doubles.
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;
There are 3 portable values you can return from the initial invocation of
main(): 0, EXIT_SUCCESS and EXIT_FAILURE. The first 2 indicate success,
the last 2 are macros defined in <stdlib.h>.
Lawrence