bsder wrote:
> Lawrence Kirby wrote:
>
>> On Thu, 14 Jul 2005 05:48:23 +0000, bsder wrote:
>>
>>
>>> Hi,
>>>
>>> Can anyone please tell me how to calculate the size of the following
>>> 4-dimensional array, and now to use qsort for sorting on this array?
>>
>>
>>
>> You'll need to explain what you mean by "sorting" a 4-dimensional array.
>> Sorting is inherently a 1D process, there is no single way in which a 4D
>> array might be considered "sorted".
>>
>>
>>> double sp[3] = { 4.0, 5.0, 6.0 };
>>> double spa[3][2] = {
>>> { 4.0, 2.0 },
>>> { 5.0, 8.0 },
>>> { 6.0, 6.0 },
>>> };
>>>
>>> double spb[3][2][2] = {
>>> { {1.0, 2.0}, {3.0, 4.0} },
>>> { {5.0, 6.0}, {7.0, 8.0} },
>>> { {9.0, 10.0 }, {11.0, 12.0} },
>>> };
>>>
>>> // spc(Time, X, Y, Z)
>>> double spc[3][1][1][1] = {
>>> { {{1.0}} },
>>> { {{5.0}} },
>>> { {{9.0}} },
>>> };
>>
>>
>>
>> In this case it is fairly obvious because the array is degenerate: only
>> one dimension has a size other then 1. Here you could write
>>
>> qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
>>
>> This works when other dimensions are greater than 1, as long as you are
>> just viewing the array as a 1D array of "rows" where each row happens to
>> be an array in its own right. The comparison function will need to sort
>> out the details of how to compare 2 complete rows in a valid way.
>>
>
> I feel abit trouble creating a "compare" function for the comparison of
> two different 2D arrays. Is there any simple example I can follow?
>
Here is what I got now:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
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);
}
void prn_distance(double spb[][4], int n)
{
register int x,y,z, t,coor=0;
double distance, total;
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
> Thanks
> D
>
>> Lawrence