Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > size of multi-dimensional array and qsort

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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

 
Reply With Quote
 
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

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      07-14-2005
In article <0VrBe.48530$>,
bsder <> 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.

 
Reply With Quote
 
bsder
Guest
Posts: n/a
 
      07-14-2005
Kenny McCormack wrote:
> In article <0VrBe.48530$>,
> bsder <> 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?
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any way to refer to another array in qsort's compare function? Stone C Programming 23 01-17-2012 10:35 AM
sorting an array of strings using qsort Cstudent C Programming 1 05-10-2009 04:04 AM
Problem by sorting array of structures using qsort zavnobih@gmail.com C Programming 6 03-21-2007 02:07 AM
qsort on multi-dimensional array bsder C Programming 1 07-14-2005 05:43 PM
How to declare an array of sort functions for qsort()? Ingo Brueckl C Programming 5 12-19-2003 01:08 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57