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%
        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)%
        500
        250
Two-Dimensional & Multi-Dimensional Arrays:
        DIM twoarray%(10, 10)
        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%
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%
        1
        2
        3
        ...
        80
        90
        100
        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     |