LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to access a four dimensional array? (https://www.linuxquestions.org/questions/programming-9/how-to-access-a-four-dimensional-array-809690/)

archieval 05-23-2010 09:51 PM

How to access a four dimensional array?
 
Hello,

I have never used multidimensional array for a long time so please consider this as a newbie question. I am editing a program that uses a 4 dimensional array and I think I'm losing track of the indexes.

day = 0 to 30
hour = 0 to 23
channel = 0 to 7
len = 0 to 63

unsigned char idx[day][channel][hour][len];

I have seen that the assignment to a string is

snprintf(idx[day][hour][channel], 64, "%s/%s", dayofmonth, hourofday);

Is this correct?

Because when I access with a day argument, that day value goes into the hour part.

Regards,
archieval

pixellany 05-23-2010 10:03 PM

That does not look like a "4-dimensional array". What I see is a simple structure with each entry having the four elements.

For the data ranges you show, a 4-D array would have 31*24*8*1 = 5952 discrete points.

mac.tieu 05-23-2010 10:03 PM

Quote:

Originally Posted by archieval (Post 3978924)
Hello,

I have never used multidimensional array for a long time so please consider this as a newbie question. I am editing a program that uses a 4 dimensional array and I think I'm losing track of the indexes.

day = 0 to 30
hour = 0 to 23
channel = 0 to 7
len = 64

unsigned char idx[day][channel][hour][len];

I have seen that the assignment to a string is

snprintf(idx[day][hour][channel], 64, "%s/%s", dayofmonth, hourofday);

Is this correct?

Because when I access with a day argument, that day value goes into the hour part.

Regards,
archieval

Declaration and usage should be in the same order.

MT

archieval 05-23-2010 10:15 PM

Quote:

Originally Posted by pixellany (Post 3978927)
That does not look like a "4-dimensional array". What I see is a simple structure with each entry having the four elements.

For the data ranges you show, a 4-D array would have 31*24*8*1 = 5952 discrete points.

Sorry, I changed len 0 to 63, now it's a four dimensional array?

Regards,
archieval

archieval 05-23-2010 10:22 PM

Quote:

Originally Posted by mac.tieu (Post 3978928)
Declaration and usage should be in the same order.

MT

It should be like this?
snprintf(idx[day][channel][hour], 64, "%s/%s", dayofmonth, hourofday);

mac.tieu 05-23-2010 10:55 PM

Quote:

Originally Posted by archieval (Post 3978938)
It should be like this?
snprintf(idx[day][channel][hour], 64, "%s/%s", dayofmonth, hourofday);

Yes, that's right.

pixellany 05-24-2010 03:50 PM

To me, a "4-dimensional array" is a structure with 4 numerical indices, where a particular combination of indices produces a value (or another structure)

It seems like you want to use the ranges you specified as the data, and not as the array indices.

fruttenboel 05-24-2010 04:36 PM

Quote:

Originally Posted by archieval (Post 3978924)
Hello,

I have never used multidimensional array for a long time so please consider this as a newbie question. I am editing a program that uses a 4 dimensional array and I think I'm losing track of the indexes.

day = 0 to 30
hour = 0 to 23
channel = 0 to 7
len = 0 to 63

unsigned char idx[day][channel][hour][len];

I have seen that the assignment to a string is

snprintf(idx[day][hour][channel], 64, "%s/%s", dayofmonth, hourofday);

Is this correct?

Because when I access with a day argument, that day value goes into the hour part.

Regards,
archieval

Try it in a proper language, where the compiler is HELPING you, instead of confusing you.

http://fruttenboel.verhoeven272.nl/mocka/index.html

archieval 05-24-2010 09:15 PM

Quote:

Originally Posted by pixellany (Post 3979857)
To me, a "4-dimensional array" is a structure with 4 numerical indices, where a particular combination of indices produces a value (or another structure)

It seems like you want to use the ranges you specified as the data, and not as the array indices.

Ah I see, ok now I get what you mean. Or perhaps a little bit. I am using the day, channel, and hour indices combination as a "single variable" to hold a string (the string is a file path that contains directory name that specifies the day, and the actual file that contains the channel number and hour number). So the purpose of that array is only to hold a string. So it's not really "4 dimensional", but rather a 3 dimensional array that holds a string. Thanks for the clarification.

Regards,
archieval

archieval 05-24-2010 09:18 PM

Quote:

Originally Posted by fruttenboel (Post 3979908)
Try it in a proper language, where the compiler is HELPING you, instead of confusing you.

http://fruttenboel.verhoeven272.nl/mocka/index.html

What is the purpose of this? I am using a specific cross compiler because I am working on embedded systems. I cannot add this module on my compiler.

Regards,
archieval


All times are GMT -5. The time now is 03:02 AM.