Advertisement

Multidimensional Arrays

Started by April 13, 2001 12:38 PM
0 comments, last by AMoronThatWantsToProgram 23 years, 9 months ago
I am having a problem with my little tile engine I created. I need a function to find the coordinates in the array. for example: lets say I have a array of int a[3][3][3]; if the first element is given 0 next 1 next 2 ....so on. now say I had to find the cordinates(a[x][y][z]) of the nth element. How would I do this. I asked my calculas teacher who is also a programmer said there was a formula for this but he didnt remember it fully. Thanks
Rrright

For a three * three * three array, it is simple.

Say, you want to find 14...

First, devide it into three seperate layers. First contains the numbers 0 - 8, second 9 - 17, and third 18-26. Only the first numbers (0, 9 and 18) are important... Look for the largest number you can substract. In this case, it is 9. So you are in the second layer. Now, substract 9 from your original number. 14 - 9 = 5. Remember the 2...

Now, do the same thing. Devide into three. You get the numbers 0 - 2, 2 - 5, 6 - 8. Look for the largest (first) number you can substract... That would be 2. 5 - 2 = 3. Remember the 2.

Now you got one row left. You still have three places to go. So the last number will be 3.

So, the coordinates of 14 would be [2][2][3]

Can you follow this???

This topic is closed to new replies.

Advertisement