Velocity Reviews > size of multi-dimensional array and qsort

# size of multi-dimensional array and qsort

bsder
Guest
Posts: n/a

 07-14-2005
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?

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}} },
};

for (int t=0; t<max_time; t++)
for (int x=0; x<max_x; x++)
for (int y=0; y<max_y; y++)
for (int z=0; z<max_z; z++)
qsort(...);

Thanks
D

Michael Mair
Guest
Posts: n/a

 07-14-2005
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?
>
> 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}} },
> };

Sizes:
sizeof sp
sizeof spa
sizeof spb
sizeof spc
If you want the number of elements per first dimension, you can use
sizeof sp/sizeof sp[0].

>
>
> for (int t=0; t<max_time; t++)
> for (int x=0; x<max_x; x++)
> for (int y=0; y<max_y; y++)
> for (int z=0; z<max_z; z++)
> qsort(...);

The problem is: How do you want to sort?
You seem to want to sort along the highest dimension of spc:
If yes, pass spc[t][x][y] to qsort() along with a comparison
function for double* to achieve it.
If no, define "order" on a n-dimensional array with all dimensions
but one fixed or provide transformations to and from a 1D array
along with appropriate comparison functions.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

bsder
Guest
Posts: n/a

 07-14-2005
Michael Mair wrote:
> 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?
>>
>> 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}} },
>> };

>
>
> Sizes:
> sizeof sp
> sizeof spa
> sizeof spb
> sizeof spc
> If you want the number of elements per first dimension, you can use
> sizeof sp/sizeof sp[0].
>
>>
>>
>> for (int t=0; t<max_time; t++)
>> for (int x=0; x<max_x; x++)
>> for (int y=0; y<max_y; y++)
>> for (int z=0; z<max_z; z++)
>> qsort(...);

>
>
> The problem is: How do you want to sort?
> You seem to want to sort along the highest dimension of spc:
> If yes, pass spc[t][x][y] to qsort() along with a comparison
> function for double* to achieve it.
> If no, define "order" on a n-dimensional array with all dimensions
> but one fixed or provide transformations to and from a 1D array
> along with appropriate comparison functions.
>
> Cheers
> Michael

Hi, I wrote the following version of passing 4-dimensional array in to a
function for printing, but there is an error when passing a
4-dimensional array in to the function.
Here is the code:

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

void prn_sorted_distance(double *spc)
{
//qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
}

void prn_distance(double *spb)
{
register int a,b,c, t,x,y,z;
x=y=z=0;
double tmp_x, tmp_y, tmp_z, distance, total;
int size_T = sizeof spb / sizeof spb[0];
int size_X = sizeof spb[0] / sizeof spb[0][0];
int size_Y = sizeof spb[0][0] / sizeof spb[0][0][0];
int size_Z = sizeof spb[0][0][0] / sizeof spb[0][0][0][0];
int x_co_ord, y_co_ord, z_co_ord;
for ( t = 0; t < size_T; t++ ) {
for ( x = 0; x < size_X; x++ ) {
tmp_x = 100.00-spb[t][x][y][z];
//printf("x: %6.1lf\n", spb[t][x][y][z]);
for ( y = 0; y < size_Y; y++ ) {
tmp_y = 100.00-spb[t][x][y][z];
//printf("y: %6.1lf\n", spb[t][x][y][z]);
for ( z = 0; z < size_Z; z++ ) {
printf("z: %6.1lf; ", spb[t][x][y][z]);
tmp_z = 100.00-spb[t][x][y][z];
a = tmp_x * tmp_x;
b = tmp_y * tmp_y;
c = tmp_z * tmp_z;
total = a + b + c;
distance = sqrt(total);
printf("distance: %6.1lf\n", distance);
}
}
}
}
}

int main()
{
double spb[3][1][1][1] = {
{ {{1.0}} },
{ {{5.0}} },
{ {{9.0}} },
};

prn_distance(&spb);
//prn_sort_distance(spb);

return 1;
}

I haven't implement the function for comparison. The comparison will be
based on distance of two points. But I need to solve the array passing
by reference first.

Thanks
D

Lawrence Kirby
Guest
Posts: n/a

 07-14-2005
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.

Lawrence

bsder
Guest
Posts: n/a

 07-14-2005
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?

Thanks
D
> Lawrence

bsder
Guest
Posts: n/a

 07-14-2005
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

bsder
Guest
Posts: n/a

 07-14-2005
bsder wrote:
> 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;
> }
>

I actually want to compare the distance of each row in this 2D array.

> Thanks
>
>> Thanks
>> D
>>
>>> Lawrence

Kenny McCormack
Guest
Posts: n/a

 07-14-2005
In article <0VrBe.48530\$(E-Mail Removed)>,
bsder <(E-Mail Removed)> wrote:
.... (mucho el code grande - snipped)
>I haven't implement the function for comparison. The comparison will be
>based on distance of two points. But I need to solve the array passing
>by reference first.

Thanks for keeping us posted. Good luck in your endeavors.

>Thanks
>D

Oh, no, thank *you*! You've done all the work. We appreciate it.

bsder
Guest
Posts: n/a

 07-14-2005
Kenny McCormack wrote:
> In article <0VrBe.48530\$(E-Mail Removed)>,
> bsder <(E-Mail Removed)> wrote:
> ... (mucho el code grande - snipped)
>
>>I haven't implement the function for comparison. The comparison will be
>>based on distance of two points. But I need to solve the array passing
>>by reference first.

>
>
> Thanks for keeping us posted. Good luck in your endeavors.
>
>
>>Thanks
>>D

>
>
> Oh, no, thank *you*! You've done all the work. We appreciate it.
>

Execuse me, what do you meant?

bsder
Guest
Posts: n/a

 07-14-2005
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".
Can anyone please point me to the direction how to correct this 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