473,602 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size of multi-dimensional array and qsort

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
Nov 15 '05 #1
18 2450
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.
Nov 15 '05 #2
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
Nov 15 '05 #3
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_dist ance(double *spc)
{
//qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
}

void prn_distance(do uble *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("distanc e: %6.1lf\n", distance);
}
}
}
}
}

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

prn_distance(&s pb);
//prn_sort_distan ce(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
Nov 15 '05 #4
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

Nov 15 '05 #5
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_dist ance(double spc[][4], int n)
{
//qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
printf("size_sp c*: %d\n", sizeof *spc);
qsort(spc, n, sizeof *spc, compare);
}

void prn_distance(do uble 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("distanc e: %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(sp b, size_elem);
printf("--------------------------\n");
printf("size_sp c: %d\n", sizeof spb/sizeof *spb);
printf("size_sp c*: %d\n", sizeof *spb);
prn_sorted_dist ance(spb, size_elem);

return 1;
}

Thanks Thanks
D
Lawrence

Nov 15 '05 #6
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_dist ance(double spc[][4], int n)
{
//qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
printf("size_sp c*: %d\n", sizeof *spc);
qsort(spc, n, sizeof *spc, compare);
}

void prn_distance(do uble 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("distanc e: %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(sp b, size_elem);
printf("--------------------------\n");
printf("size_sp c: %d\n", sizeof spb/sizeof *spb);
printf("size_sp c*: %d\n", sizeof *spb);
prn_sorted_dist ance(spb, size_elem);

return 1;
}

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

Nov 15 '05 #7
In article <0V************ ******@news-server.bigpond. net.au>,
bsder <bs***@bsder.co m> 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.

Nov 15 '05 #8
Kenny McCormack wrote:
In article <0V************ ******@news-server.bigpond. net.au>,
bsder <bs***@bsder.co m> 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?
Nov 15 '05 #9
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_dist ance(double spc[][4], int n)
{
//qsort(spc, sizeof spc/sizeof *spc, sizeof *spc, compare);
printf("size_sp c*: %d\n", sizeof *spc);
qsort(spc, n, sizeof *spc, compare);
prn_distance(sp b, n);
}

void prn_distance(do uble 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("distanc e: %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(sp b, size_elem);
printf("--------------------------\n");
printf("size_sp c: %d\n", sizeof spb/sizeof *spb);
printf("size_sp c*: %d\n", sizeof *spb);
prn_sorted_dist ance(spb, size_elem);

return 1;
}

Thanks
D
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6039
by: Mark | last post by:
hello! normally, if you are given binary data in a string (such as from a call to fread), you can call the strlen function on this string to get its size: $buffer = fread($file, 100000); if (strlen($buffer) < 100000) { echo "read less than 100000 bytes"; }
3
2971
by: Bilal | last post by:
Hi, I've been looking all over the net for a way to increase the size of a drop down menu without any success. Does anyone perhaps have a way to display 11-15 items on a menu without having recourse to: 1. scroll bar. 2. configuring IE 3. decreasing the size of the font. Here is the code I used for my Drop down Menu: ______________ <select style="background-color: #FFFFFF; width:300;" size="1"
38
2352
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
3
3248
by: Linh Luong | last post by:
Hi All, 1. I have been reading and the max size of a tuple is 8K. I have also read that I can it to a larger size in some config file. Where is this file? is it called pg_config.h and is the variable called BLKSZ?? 2. Is there a way I can find the actual size of the tuple? Do you go into each column and find the length of each value and sum it up? I am out of ideas.. if someone knows how please shine some light on my situation..
14
2747
by: googler | last post by:
Is there any C library function that returns the size of a given file? Otherwise, is there a way in which file size can be determined in a C program? I need to get this for both Linux and Windows platforms, so a generic solution is what I am looking for. Thanks for your help.
11
17642
by: ganesh.kundapur | last post by:
Hi, Is there any way to get the total heap size allocated to a process in c on linux platform. Regards, Ganesh
4
3656
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating properly (I have a file that has a size of 1 KB but has 14 bytes in it so the conversion isn't working right). Is there some method/property out there that will get the actual size of the file? Also, would there be a method like this that will...
7
43878
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics object. 4. If string size is less than the size of the rectangle, we are done.
5
11102
by: sameer_deshpande | last post by:
Hi, I have DB2 UTF-8 database (codeset IBM-1252) on Windows 2000 Server. I need to store multi-byte contents. How do I calculate size of the column to store multi-byte information? F.ex: I cant have a column with size VARCHAR(10) bytes to store 10 multi-byte characters, because its going to run out of column size. Do I have to simply double/tripple the column size simply by guessing?
7
5613
by: Raman | last post by:
Hi All, Could any one tell me, how can I determine/Change size of heap on per- process basis on Unix based systems. Thanks. Regards
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.