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   

Two Dimensions, One Dimension

Let's say you have a two dimensional array: MyArray(9,9). That's an array with 100 elements; starting with MyArray(0,0), all the way to MyArray(9,9) and everything in between. Suppose you wanted to treat the elements of this array as a one dimensional array. In other words, you wanted to think of the array like this:
Element  #0: MyArray(0,0)
Element  #1: MyArray(0,1)
Element  #2: MyArray(0,2)
...
Element  #9: MyArray(0,9)
Element #10: MyArray(1,0)
Element #11: MyArray(1,1)
...
Element #97: MyArray(9,7)
Element #98: MyArray(9,8)
Element #98: MyArray(9,9)
This is a situation you may run into from time to time, and it's good to be able to "walk through" a two dimensional array, assigning a one-dimensional index to each element. Now, for this example, I've picked something easy, because you'll notice that the ones and tens places of the one-dimensional index correspond to the X and Y dimensions of the two-dimensional indices. What's the one-dimensional index for MyArray(7,8)? It's easy! Put the 7 and the 8 together, and you've got Element #78.

But we need to express that mathematically, so the computer can understand it.

What is the "tens" place of a number? The tens place tells you "how many tens" are in a number. For example, the number 78 is "seven tens and eight more", or 7 x 10 + 8. Great! That tells us how to create our one-dimensional index! Check this out:

Dim MyArray(9,9)
Dim X As Integer
Dim Y As Integer

'Get the one-dimensional index
Public Function ArrayIndex(Y As Integer, X As Integer)
   ArrayIndex = (Y * 10) + X
End Function

That's it! Notice that I put the line of code inside a function. This is because I'll probably be using that line of code a lot, and I don't want to have to keep rewriting it over and over again. Plus, if I ever change the array to different dimensions, like maybe a 20x20 array, I would need to change the code, and having it inside a function means I only have to change it in one place.

Speaking of different dimensions, let's suppose MyArray now is a 7x7 array. How does that change our code? Well, that changes it just a little bit:

Dim MyArray(6,6)
Dim X As Integer
Dim Y As Integer

'Get the one-dimensional index
Public Function ArrayIndex(Y As Integer, X As Integer)
   ArrayIndex = (Y * 7) + X
End Function

Easy, right? In my next tip, I'll talk about how to reverse the process and convert a one dimensional array index into a two dimensional array index.

Other Scenarios

  1. Write a function to get an index into the array MyArray(12,15)
  2. Write a function to get an index into the array MyArray(8,10)

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