# One Dimension, Two Dimensions

Reference > Science > Technology > Beginner Programming TipsIn my previous tip, I talked about how to find a one dimensional index into a two dimensional array. If you are ever in a situation where you need to do that, there's a good chance you'll need to do the *opposite* as well!

Let's say you have a one-dimensional array MyArray(99). This is an array with 100 elements. But you want to think of these 100 elements as being laid out in a 10x10 grid, assigning X and Y "coordinates" to each one:

MyArray(0) (0,0) MyArray(1) (0,1) MyArray(2) (0,2) ... MyArray(9) (0,9) MyArray(10) (1,0) MyArray(11) (1,1) ... MyArray(97) (9,7) MyArray(98) (9,8) MyArray(99) (9,9)

To do this, there are two nice little operations you should know about. Here they are:**Integer Division: **Normally when you divide, you write your division like this:

But there's another way to divide, and it looks like this:

What does this do? It takes the integer part of the division Y / 10. In other words, it's essentially the same as writing:

**Modulus** The Mod operator calculates the *remainder*when one value is divided by another. For example:

When you divide 12 by 7, you get an answer of 1, with a remainder of 5. So in this example, X would equal 5.

Now, with these two tools in our toolbox, we can write the following functions:

'Get the X Coordinate

Public Function XCoordinate(Index As Integer)

XCoordinate = Index Mod 10

End Function

'Get the Y Coordinate

Public Function YCoordinate(Index As Integer)

YCoordinate = Index \ 10

End Function

This time I have *two* functions, because in converting the one dimensional array to a two dimensional array, the array index has two coordinates instead of just one.

Now suppose, instead of our easy 10x10 array, we've got a 5x12 array. How does that change our code? Take a look, and make sure you understand*why* it has changed this way.

'Get the X Coordinate

Public Function XCoordinate(Index As Integer)

XCoordinate = Index Mod 12

End Function

'Get the Y Coordinate

Public Function YCoordinate(Index As Integer)

YCoordinate = Index \ 12

âEnd Function

In both functions we used the *twelve*, but not the *five*! Of course, this is because we wanted to lay out the array as a 5x12 "grid". If we wanted to lay it out as a 12x5 grid, do you think we would have used the 5 instead? You bet we would!

## Questions

# Ask Professor Puzzler

Do you have a question you would like to ask Professor Puzzler? Click here to ask your question!**Get a**

*FREE*Pro-Membership!Educators can get a free membership simply by sharing an original lesson plan on our

*Articles for Educators*page!