Search:
Home Math Games Word Games Strategy Reference Junior Games Daily Puzzles Printables Problems Members

Home Reference Programming Tips

Each page contains a helpful programming tip and exercises which encourage beginners to use what they've learned in a different situation.
Index Previous    Next    Teachers    About   

One Dimension, Two Dimensions

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 remainder when 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 understand why 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!

Other Scenarios

To make sure you understand modulus math, calculate the following values, then use your computer to check your answers.
  1. X = 20 Mod 5
  2. X = 20 Mod 7
  3. X = 15 Mod 12
  4. X = 8 Mod 7
  5. X = 7 Mod 8
To make sure you understand integer division, calculate the following values, then use your computer to check your answers.
  1. X = 20 \ 5
  2. X = 20 \ 7
  3. X = 15 \ 12
  4. X = 8 \ 7
  5. X = 7 \ 8

Index Previous    Next    Teachers    About   

"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 



 
Search For More Resources Search For More Educational Resources

 

Top Games On This Site

 
Adders!
Scramble
Entrapment
Secret Word
Trio Match
 

Just a few of the educational game resources available on The Problem Site!

 
 






Home       All Games       Problems       Contact       Site History       Privacy Policy       Member Page