Arrays

        Arrays are the easiest way (in Qbasic) to store large amounts of data, ie: numbers or words, if we didn't have arrays we would have to have a seperate variable for each value, and that could be thousands. How would you like to write thousands of variables out? Probably wouldn't.
        First of all I will give you a basic definition. An array is a collection of values, it's like a list of values all strung together. To use an array is we must first declare it, which can be done by:
        DIM array%(10)
        Okay what this line of code says is: Dimension an array named 'array' of type integer (indicated by %) with 10 elements. Each value stored in an array is called an element. The way to access that element is to use it's Index number, where the index number is the number that is enclosed in the parenthesis. In the example above you can access index numbers from 1 to 10. There are no index numbers before this or after range of ten numbers. If you did try to access say index number 11 then you would get an error like:"subscript out of range"
        The next thing that we would like to do is put values into the array elements, that is 'Initalize' the array. This can be done by, in many ways, for my example we'll use a FOR loop to initalize the array elements to ten times their index values. This can be done by:
        FOR X% = 1 to 10
          array%(X%) = X% * 10
        NEXT X%

        In this Example, as you can see, we just go through the loop and use the variable used as the 'loop counter' to indicate which element we wish to access. So, for each iteration we are accessing a different element in the array. We are storing a value of ten times the index number, however, we can store any number that we want to in any element of the array. Remember an array is just like a variable, it just stores more values.
        Now that the array is full of values, we can do something with it. You can treat an array just as if it were a variable, you just need to keep in mind you will get wierd errors if you do not index to the correct element. From the example above, say we want to change the value of 500, which is stored in array%(5) to 225, we would do that by:

        PRINT array(5)%
        array%(5) = array(5)% / 2
        PRINT array(5)%

Output:
        500
        250

        As you can see, you would treat an array just as if it were a variable. Just remember to access the right element.

Two-Dimensional & Multi-Dimensional Arrays:

        Now to introduce you to something wierder on the subject of arrays, 'two-dimensional arrays'. The best Analogy that I can think of to explain these types of arrays is this: Say that you have a piece of paper, and you draw a grid on it. Then you number the grid across (1, 2, 3, ect...) and down (1, 2, 3, ect...). Now you start writing numbers in all of these grid boxes. So to find to a perticular number you would look at the number across and the number down to tell where the number that you are looking for is. Your value will be located at grid position (X%, Y%). Okay, now to translate that analogy into a 2D array. To declare a two-dimensional array we would do almost just like the first example in this section:
        DIM twoarray%(10, 10)

        Well good news, it's easy. As you can see all you have to do is add a comma and the second index value. The second index value does not have to be the same as the first. Also two-dimensional arrays follow the same rules that normal arrays follow.
        The main difference in the 2D Vs. 1D arrays is how you access and initalize them. To initalize a 2D array you need to use a 'nested' for loop. Where nested means that you have a loop inside of a loop. Here is how you would initalize the array from above to it's index values multiplied together:

        FOR X% = 1 to 10
          FOR Y% = 1 to 10
            twoarray%(X%, Y%) = X% * Y%
          NEXT Y%
        NEXT X%

        First of all let me explain what exactly is happening here. For the first iteration of the outside loop the inside loop is executed. So, the way in which the array is initalized is demonstrated in the list below:
1) twoarray%(1, 1) = 1 * 1
2) twoarray%(1, 2) = 1 * 2
...
10) twoarray%(1, 10) = 1 * 10
11) twoarray%(2, 1) = 2 * 1
12) twoarray%(2, 2) = 2 * 2
...
100) twoarray%(10, 10) = 10 * 10

        The list above shows kind of how the interpreter sees it, this is a very basic example of a 10x10 multiplication table. To display the values you would do this:

        FOR X% = 1 to 10
          FOR Y% = 1 to 10
            PRINT twoarray%(X%, Y%)
          NEXT Y%
        NEXT X%

Output:
        1
        2
        3
        ...
        80
        90
        100

        I reccomend that you try this example and see how this works, Also one way to see what is going on inside of a program is to place print statements in various places, like inside of loops to see what the values of the variables are. This is also a good way to debug your code.
        Now we come to multi-dimensional arrays. It is very likely that the greatest dimension that you will work with is 3 or 4 maybe. However there is support for up to 60 (in some cases) dimensions. To use those you just add another comma and index number in the definition for each extra dimension, as with the 2D arrays, and for initalization all you need to do is add another loop inside the already existing loops for each dimension. Just play around with these, it will become clear to you after seeing what they do.

    Back         Home         Next