## How to Make 3+ Dimensional Arrays Edit

Many people are under the misconception that it is impossible to have an array of more than two dimensions on PTC for DSi. This is syntactically true, the interpreter does not allow one to define a three dimensional array directly with the `DIM`

instruction, but with a little creativity a one or two dimensional array can be turned implicitly into an array of almost any number of dimensions.

To start we will make a two dimensional array `ARRAY2D(X_SIZE,Y_SIZE)`

using a one-dimensional array. It is quite simple here is the code:

`DIM ARRAY2D(X_SIZE*Y_SIZE)`

That is all! In order to find value (4,2) in that array you can substitute `ARRAY2D(4+X_SIZE*2)`

where `X_SIZE`

is the number of values in the X dimension of the entire array. This works because a two dimensional array can be thought of as a one dimensional array divided into segments. The Y dimension represents which segment you are choosing and the X dimension represents which value in that segment you are choosing. The length of each segment is equal to the X value used in `DIM`

. Because of this by multiplying the Y value you want by the number of x-values (the length of each segment) and adding the X value you want you get the equivalent to (X,Y) in a one dimensional array. So, in general, the element at (X,Y) is `ARRAY2D(X+X_SIZE*Y)`

.

This can be applied to a three dimensional array too using the code:

`DIM ARRAY3D(X_SIZE*Y_SIZE*Z_SIZE)`

The value for (2,3,5) is then retrieved with `ARRAY3D(2+X_SIZE*(3+Y_SIZE*5))`

. Once again think of segments only this time the array is divided into Z segments made of the Y segments discussed earlier. You can keep on adding dimensions from here until you start hitting the limit for the number of values in a PTC array. In general, the element at (X,Y,Z) is `ARRAY3D(X+X_SIZE*(Y+Y_SIZE*Z))`

. For 4D, (X,Y,Z,W) would be `ARRAY4D(X+X_SIZE*(Y+Y_SIZE*(Z+Z_SIZE*W)))`

, etc..

One important aspect of implementing dimensions in this manner is that individual range checking on each index is not enforced by BASIC. If a regular 2D array is declared with `DIM ARRAY2D[X_SIZE,Y_SIZE]`

, then any reference to `ARRAY2D[-1,1]`

, for instance, will generate a `Subscript out of range`

error, but if it is declared as above, a reference to `ARRAY2D[-1+X_SIZE*1]`

will not.

## CreditsEdit

alfanzothehutt