0

Hello I'm fairly new to programming. I took an intro to Java class and I'm taking c++.

My question is, in a 3D array:

int myArr[a][b][c];

which is the one that defines the layers? a or c?

3
  • 5
    What do you mean by "define the layers?" Commented Oct 23, 2013 at 1:52
  • 2
    Spiral rule: myArr is an array of a arrays of b arrays of c ints. Commented Oct 23, 2013 at 1:54
  • 1
    Something you may need to know, but the entire array will be stored sequentially in memory. Commented Oct 23, 2013 at 2:59

3 Answers 3

4

The way arrays are allocated in C++, you will have a blocks, each pointing to a b block each pointing to a c block. Like this:

 a -> b -> c
           c
           c
      b -> c
           c
           c

So, I would say a is defining the layers.

Sign up to request clarification or add additional context in comments.

Comments

3

a would be the variable defining the number of "layers", if you mean each "layer" is a 2d grid of b x c elements.

1 Comment

so in an array [2][3][4] the array is of 3x4 and has 2 layers?
1

When declaring and using a multi-dimensional array, it's up to you to determine which each level of the array means.

int myArray[6][3][9]

Given this array, just remember that the first index (closest to the variable name) has indicies 0-5, the middle index has indicies 0-2, and the last index has indicies 0-8. As long as you are in bounds for each, you will be fine.

3 Comments

There is more to it than that. When you're iterating through a large array (of more than one dimension) iterating through the elements in the same order they appear in memory can improve cache usage and therefore give much higher performance (I've seen more than 10:1) speed differences.
so I can make the 9 be my number of layers as well as the 6?
@Fert09 There's no restriction on indicies other than memory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.