## Beginning Programming 4 - Section Three

### by Brad Moore

Newsletter Home | Beginning Programming 4 Home | Section One | Section Two | Section Three | Section Four | Glossary

### The Final Frontier

To Boldly Go...

Well this is not the end of the series or anything, just that we are starting a project having to do with Space - The Final Frontier. If you are a "Trekkie" you get it (right Mr. Drake?). For the remainder of the article we are going to build on our understanding of arrays by introducing double dimensioned arrays. As you may remember from parts 2 and 3 of the series, arrays are simply multiple instances of a variable, each instance being unique but intimately related. The instances of the array, usually called elements are specified by the array variable name followed by the array subscript. The subscript is used to uniquely identify each of the array elements. Here is an example:

    Toys(15)

"Toys" is the name if the array and the "(15)" is the subscript that uniquely identifies this specific element of the "Toys" array. If you need to understand arrays better, go back and read about them in parts 2 and 3 of this series.

So far we have only dealt with array variables that have a single subscript. Remember these are like a string of identical mail boxes on a rail. Each with a different number. We call these types of arrays a single dimensioned array. This is because like the mail boxes, the start in one place and go out in one direction (in the case of the mail boxes horizontally).

Today we are going to expand on that by adding a second direction to the array. At this point our rural mail box analogy from the earlier articles kind of breaks down, but don't loose heart - we will turn to another classic mail device of popular cinema. Do remember the old time movies where the movie star will check into the hotel. Upon arrival the says to the desk attendant "Are there any messages for me?". What does the attendant do? She turns around and looks into a wall of little cubical. There are fifteen or twenty across and four or five up, forming a complex of little boxes that totals forty or fifty.

That "array" of little boxes has two dimensions. There is a horizontal dimension (going across) and a vertical dimension (going up and down). If this were an array we would call it a two dimensional array. It is written with the array name and two subscripts in parentheses like so:

    Mailboxes(12,2)

The subscripts are almost like an address. They direct us to the cell (or better yet element of the array) that we need to examine of store data into. In the case of our array above, we are directed to the twelfth position across and the second position up. Maybe in the hotel scenario we will find a slip of paper there with our message. Maybe we want to put a message there. The message is like the value that is stored in the variable. In this case it is stored in a two dimension array variable.

Like other arrays, we must explicitly create these before we use them. There are no exceptions for two dimensional arrays, like the implicit declaration of an array with ten or fewer elements. ALL two dimension array variables must be declared with the DIM statement. In the case of our Mailboxes array above (which we will say has a maximum size of 20 across and 6 up) we would use the following to create the array variable and the memory space for the array:

    dim Mailboxes(20,6)

So, if that is the case, lets populate the array with some random values between 10 and 99. We will do this with a couple of nested FOR-NEXT loops. Here is the code:

    'declare the array
    dim Mailboxes(20,6)

    'now populate the array
    for x = 1 to 20
       for y = 1 to 6
          Mailboxes(x,y) = int(rnd(1)*89)+10
       next y
    next x

So now if we want to examine the contents of the array, we can also dump the whole thing to the screen using a set of nested FOR-NEXT loops again...

    'dump the contents of the Mailboxes
    'now populate the array
    for x = 1 to 20
       for y = 1 to 6
          print "(";x;",";y;") ";Mailboxes(x,y)
       next y
    next x

Notice in the print statement that I have imbedded the array elements so that you can see what is going on. When run you will get a long list of element numbers and their values. Now consider printing them out in a matrix, similar to our mailbox cubby holes that we have in out analogy. This is not particularly hard. For it to turn out right, we must reverse the order we are examining the values in, and increment the second idiocy (or subscript number) before the first. This will allow us to print all of row 1, then all of row 2, then all of row 3 and so on. Here is the code to print these out:

    for y = 1 to 6
       for x = 1 to 20
          print Mailboxes(x,y);" ";
       next x
       print
    next y

Did you notice the semicolon ";" at the end of the print statement. This will cause the next value to be printed immediately following the first, on the same line. That will cause all 20 elements of a given row to print on that row. Also we needed a extra print statement between the two next statements to break the string together of the values. Try running the code after removing this print statement and see what you get. The output we really want should look like the following:

    69 36 16 63 55 44 21 48 96 50 30 55 56 11 62 13 60 61 74 41
    63 88 31 72 73 65 85 40 55 25 37 67 84 62 30 73 43 60 36 77
    88 19 21 91 74 86 18 16 70 15 96 85 44 22 93 87 76 55 11 59
    34 81 18 65 56 75 88 37 90 92 58 86 18 64 94 75 97 59 93 11
    65 76 82 49 46 90 69 73 87 18 57 30 12 31 64 35 79 21 70 32
    46 39 57 85 43 35 16 51 38 50 78 36 30 86 25 51 46 32 43 33

We have covered this because we are going to develop a cute little space game using the two dimensioned array. This is a program that my first Basic manual took me through developing when I was young and learning to write programs on the TRS-80 oh so many years ago. This will be out last program that we write to run in the Liberty Basic console. Once we have written this program, over the next few parts we will write it again to run in a real Windows window. Goto section four to see the next Challenge...

Newsletter Home | Beginning Programming 4 Home | Section One | Section Two | Section Three | Section Four | Glossary