Games
Problems
Go Pro!

One Dimension, Two Dimensions

Reference > Science > Technology > Beginner Programming Tips
 

In 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:

X = Y / 10


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

X = Y \ 10


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

X = Int(Y / 10)


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

X = 12 Mod 7


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:

Dim MyArray(99)

'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 understandwhy it has changed this way.
 

Dim MyArray(59)

'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

1.
Calculate: X = 20 Mod 5.
2.
Calculate: X = 20 Mod 7.
3.
Calculate: X = 15 Mod 12.
4.
Calculate: X = 8 Mod 7.
5.
Calculate: X = 7 Mod 8.
6.
Calculate: X = 20 \ 5.
7.
Calculate: X = 20 \ 7.
8.
Calculate: X = 15 \ 12.
9.
Calculate: X = 8 \ 7.
10.
Calculate: X = 7 \ 8.
Assign this reference page
Click here to assign this reference page to your students.
Two Dimensions, One DimensionTwo Dimensions, One Dimension
Removing an Array ElementRemoving an Array Element
 

Blogs on This Site

Reviews and book lists - books we love!
The site administrator fields questions from visitors.
Like us on Facebook to get updates about new resources
Home
Pro Membership
About
Privacy