Authors
Publication
Pub Details
Date
Pages
In the last few articles we started looking at some of the features of the QL’s built-in programming language, SuperBASIC. So far, we have examined some of the most rudimentary capabilities of inputting data (and accessing that data), and printing information to the screen. However, those capabilities by themselves cannot create a useful program. This month we hope to start you off in that direction and begin writing a program which you can use on your QL. With the information here (as well as past and future articles) you will be able to customize this program as well as program your own applications.
We ended the last column by discussing the use of the READ…DATA… RESTORE keywords available in SuperBASIC. Unfortunately, in order to use these, you have to know the data in advance, and insert that information directly into the DATA statements. However, this requires that the user (not always the programmer) actually get into the program and write into the proper position. This is not getting the most out of your programming. A program should operate smoothly without the end user being aware of the coding itself. In most cases, the user would not even know anything about programming if the software were to be sold to the general market. To this end, we need a way of entering a large amount of information in a manner which would give us a convenient way of entering and manipulating large amounts of information directly from the keyboard.
In order to accomplish this we need a way of having the program assign a unique variable to each bit of information we are interested in manipulating. Often we want to enter a large amount of information which needs to be related to other information. In SuperBASIC we achieve this through the use of the DIMension statement. We use this statement to create an array, or set of information boxes, into which we can type in the necessary information.
Arrays can hold either numeric or string data, although they are slightly different in use as we shall see in a moment. In order to use the array, we must first tell the computer to assign a certain amount of RAM storage in order to hold the data. This is how we would tell the computer to set aside enough room to hold 10 numbers which we will input by way of the keyboard at some time in the future.
DIMension A(9)
This statement tells the computer that you will be needing to enter 10 numbers (0-9), each of which will be identified by the variable A followed by an identifying number as we shall see. This also sets the initial value within these boxes to zero. They will remain that way until you change that value. Try the following program which allows you to enter values into this array.
10 DIMension A(9)
20 FOR F=0 to 9
30 INPUT "Enter a number ";A(F)
40 PRINT A(F)
50 NEXT F
60 PRINT
70 FOR F=0 to 9
80 PRINT "A(";F;") = ";A(F)
90 NEXT F
As you can see, we started this program off by identifying the array and initializing the values within those boxes. Next, in line 20, we use a FOR/NEXT loop to cause the program to ask you to enter a number-10 times. We tell SuperBASIC to start with the variable “F” (for F) to zero and to increase it by a value-in this case the default value of 1—each time this line is encountered by the program. We proceed to the next line-line 30-which asks for a number. The variable will change from one element of the array to the next, depending of the value of “F” ( A(F) ). The first time the program sees this line, the value of F is zero, so the value entered becomes identified as A(0). Once that is done, the program will proceed to line 40 which instructs it to print the value which you have just entered. Line 50 instructs SuperBASIC to check the value of F. If that value falls within the specified range specified in the FOR statement (in this case 0-9). If the value is within this range, the program goes back to the FOR statement (loops), which increments the value of F and continues in the loop until the value of F equals the final specified value. Once that happens (and F = 9 in our illustration) the program continues to the next line. The next portion of this short program shows you that what we have just discussed has actually taken place. The loop which we now encounter will tell you the name of each element of your array and the value which you have placed within that cell. This is an ex- ample of a one dimensional numeric array. The QL is capable of multi-dimensional arrays, limited only by the amount of memory available. (Multi-dimensional arrays will be taken up in an upcoming installment of this series.)
Strings may also be handled as arrays, with a slight difference in the use of the DIMension statement. As with a numeric array, you must also indicate the maximum length of the string. Again, this is so that the computer knows how much memory to reserve. The form would be:
DiMension NAMES(9.15)
In this statement, we use the same number of elements as we did on our numeric example (10), but we also indicate (by the second number within the parentheses) that the maximum length of each member of the array may be 15 characters. (Note: These char- acters may be either alpha-letters-or numeric, but will be treated as a string in either case.) In the case of a numeric array, the elements were all set to a value (zero) when the DIMension was declared. In a string array, each ele- ment is also established although you do not see anything since they are padded out with spaces. Try this example program which is similar to the one we used earlier.
10 DiMension NAME$(9,15)
20 FOR F=0 to 9
30 INPUT "Enter a name ";NAME$(F)
40 PRINT NAME$(F)
50 NEXT F
60 PRINT
70 FOR F=0 to 9
80 PRINT "NAME$(";:F;") = ";NAME$(F)
90 NEXT F
The same explanation as earlier holds true. When you only specify one value within the parentheses the entire string will be printed. You could specify slices of that string as follows (all examples here use only the first element entered;
PRINT NAME$(1.to 10) prints the first 10 characters only
PRINT NAME$(1.5 to) prints all characters from the 5th to the end
PRINT NAME$(1.7 to 13) prints only the 7th through the 13th characters
As we said earlier, the array may be more than the single dimension we have discussed up until this point. While the single dimension array which we have worked with so far could be thought of as a straight line with bits of data along the way, a two dimensional ar- ray would be represented as a square made up of boxes containing data. A single dimension array, A(5), could be pictured as:
A(1) A(2) A(3) A(4) A(5)
A two dimensional array, A(7,5) could be visualized in this manner:
A(1.1) A(2.1) A(3.1) A(4.1) A(5.1) A(6.1) A(7.1)
A(1.2) A(2.2) A(3.2) A(4.2) A(5.2) A(6.2) A(7.2)
A(1.3) A(2.3) A(3.3) A(4.3) A(5.3) A(6.3) A(7.3)
A(1.4) A(2.4) A(3.4) A(4.4) A(5.4) A(6.4) A(7.4)
A(1.5) A(2.5) A(3.5) A(4.5) A(5.5) A(6.5) A(7.5)
If you could imagine a third (and fourth, fifth, etc.) dimension, you could utilize arrays of many levels, dependent on the amount of available RAM. String arrays may be accommodated in the same manner, keeping in mind that there must also be—as the last number in the parentheses—a definition of the length of the string.
How might you go about using this type of multi-dimensional array? Here is an example, using the A(7,5) array which we have pictured. The 7 in the definition could be imagined to be representing the seven days of the week, while the second number, 5, could be imagined to be the possible 5 weeks which a month might cover.
| Week | Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday |
|---|---|---|---|---|---|---|---|
| 1 | A(1,1) | A(2,1) | A(3,1) | A(4,1) | A(5,1) | A(6,1) | A(7,1) |
| 2 | A(1,2) | A(2,2) | A(3,2) | A(4,2) | A(5,2) | A(6,2) | A(7,2) |
| 3 | A(1,3) | A(2,3) | A(3,3) | A(4,3) | A(5,3) | A(6,3) | A(7,3) |
| 4 | A(1,4) | A(2,4) | A(3,4) | A(4,4) | A(5,4) | A(6,4) | A(7,4) |
| 5 | A(1,5) | A(2,4) | A(3,5) | A(4,5) | A(5,5) | A(6,5) | A(7,5) |
Now, if we assume that this is a 30 day month which starts on a Sunday, we would enter the value of 1 into A(1,1), the value of 2 into A(2,1) and so forth. The 30th (and final) day of the month would be entered into A(2,5). The remaining elements, A(3,5) through A(7,5) would be left blank.
Challenge Number One
Write a program which will allow you to set up a calendar using the multi-dimensional array which we just discussed. Make it print out to either the screen, a monitor or both at least a single month including labels to identify the month, year and day of the week. Send your program to me in care of Computer Shopper and I will share the most original programs) in a future column. See your name immortalized here! Develop your programming skills and those of your fellow Sinclair enthusiasts. Be sure to describe how your program works either with separate notes, or through the use of REMark statements.
With the skills we have already learned, you now have the tools to start to create a number of useful programs which you can put to use. Starting next month, we will use these skills to start developing a name and address program which we will use to illustrate what we have already discussed, and will improve with skills which we have yet to cover.
A note to all of you who did not have the opportunity to purchase one of the original ZX-81 kits back in the days when the Sinclair computers were first making their presence felt in this con- try. They are once again available thanks to Zebra Systems (78-06 Jamaica Avenue; Woodhaven, NY 11421; (718) 296-2385), the oldest Sinclair supporting company in the U.S. Zebra Systems has obtained a quantity of these kits (containing Spanish manuals which have been replaced by English versions) and have made them available for much less than the original $200.00 price at which they first appeared. (The Spanish language manuals are also available if you wish one.)