Pseudo N-dimensional Arrays

While most programming languages support multi-dimensional arrays (although there are some that don't), there are occasions when a multi-dimensional array is not able to be used, or is impractical to use. However, there is a way to simulate a multi-dimensional array using a linear one.

Take the pseudo2d array for instance. Let's say we have a 4*2 grid, and we want to find the linear array element that corresponds to the coordinates (3,1).

If we count along the rows from left to right, starting at zero, because this method only works with zero as the starting point, we get (3,1) is in element 7. Now, we can actually use a handy little formula to replace this tedious counting. The formula for this is this:

I = width*y + x

where I is the linear index, width is (obviously) the width of the array and x and y are the coordinates we want to find the index of. If we test our coordinate (3,1) we will get the answer of index 7 as follows.

I = 4*1 + 3
  = 4 + 3
  = 7

Okay, now what if we want to find the index in a 3d array? The method is quite similar, and just as easy, although we need to do a bit more computation to find the index.

The 3d formula is this:

I = height*width*z + width*y + x

If we substitute in the coordinates (2,1,2) into the formula for a 4*2*3 array we get:

I = 2*4*2 + 4*1 + 2
  = 16 + 4 + 2
  = 22

If we try counting in our 3d grid, we'll get the same answer. Now, to make things a little easier, we'll show the grid as separate layers.

Want to try a 4d one now? Okay, we'll do one anyway. To make representing it a bit easier (given that the human mind doesn't seem to have the ability to visualize 4d space), we'll use the 3d layers from before but make separate rows of them for each 4d coordinate.

If we try to get the element at (3,1,2,1) in our 4*2*3*2 4d grid, by counting we will get 47.

Now, here's the formula to do it (w is our 4th dimensional coordinate):

I = depth*height*width*w + height*width*z + width*y + x

And if we substitute in the coordinates (3,1,2,1) we get:

I = 3*2*4*1 + 2*4*2 + 4*1 + 3
  = 24 + 16 + 4 + 3
  = 47

Which is the exact same result we got by counting.

Now, we see a pattern emerging here, don't we? By extension we can get the following general formula that will work for any pseudo-N dimensional array of dimensions d1*d2*...*dN and coordinates (c1,c2,...,cN).

I = d(N-1)*d(N-2)*...*d1*cN + d(N-2)*d(N-3)*...*d1*c(N-1) + ... + d1*c2 + c1

Feel like testing it out? Let's try something rather ridiculous that you'll probably never use, we won't bother with the counting, as it would take up too much space, and take too long (feel free to try it if you want). Let's try the number of dimensions in the universe: 11. We'll make out grid 2*2*2*2*2*2*2*2*2*2*2 and our coordinate (1,1,1,1,1,1,1,1,1,1,1).

I = 2*2*2*2*2*2*2*2*2*2*1 + 2*2*2*2*2*2*2*2*2*1 + 2*2*2*2*2*2*2*2*1 + 2*2*2*2*2*2*2*1 
      + 2*2*2*2*2*2*1 + 2*2*2*2*2*1 + 2*2*2*2*1 + 2*2*2*1 +  2*2*1 + 2*1 + 1
  = 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
  = 2047

Which you'll get if you're mad enough to try the counting method.

In conclusion, we have developed an easy way of working with multi dimensional arrays, without actually using them. Uses of this technique include handling the screen in graphics programming, and versitile ways of passing multi-dimensional arrays to functions. The latter is a way to overcome the limitations of some programming languages when passing multi-dimensional arrays to functions, such as in C, where the first dimension can be anything, but any subsequent dimensions must be fixed. Overall, this is a very useful technique and you're almost certain to quickly adopt it.

Back to the programming home page

Hosted by www.Geocities.ws

1