Timex/Sinclair Users Column

Authors

Publication

Date

Pages

A new computer owner sitting down at the keyboard, and trying to write a program for the first time may find little confusion in simple concepts such as PRINT statements, and simple math functions, but soon will find that these alone will not go very far. A single concept which is not hard to master, however, will make more useful programs possible. This simple but misunderstood programming tool is the array. In fact, all truly useful programs use some form of array. In this month’s column we will explore the use of a number of different types of arrays, and their practical uses.

In this, and all future columns concerning programing the Timex and Sinclair line of computers, most of what is discussed will work on both the 1000 series (T/S 1000, T/S 1500, and Sinclair ZX-81), and the T/S 2068 (or Sinclair ZX Spectrum). In the instances where there are differences in the two computers they will be indicated in the text as well as the program listing. Where it is possible, I will be giving methods of duplicating the T/S 2068 statements on the 1000 series.

To understand what an array is and how to use one, helps to think of it as a chart which will be filled in with various types of information. In a program, arrays are most often filled in the following manner:

10 DIM A(10) 
1000 FOR F=1 TO 10
1020 INPUT A(F)
1040 PRINT TAB 8;"A(";F;") = ";A(F)
1060 NEXT F

Let’s analyze this program line by line. Line 10 is the DIM(ension) statement necessary to let your Timex computer know that it must set aside some space in memory for a chart. In this case we are telling it that our chart will have 10 spaces to be filled in. In Sinclair BASIC this statement must appear before attempting to use any array, no matter how small the array is to be. You should, as a rule, put this statement at the beginning of your program, because if it is put into a spot where it will be repeated during the running of your program, each time the computer sees this statement it will erase all the data that you have already entered into your “A” array. Although this might be useful in some applications, for our purposes here, we will put it only at the beginning. Line 1000 sets up a FOR/NEXT loop which will repeat itself ten times. Each time through we fill another space on our chart. Line 1020 is the line which allows the data to be filled in on our chart. You will notice that we used the control variable “F” (the variable that is used to control the FOR/NEXT loop) within the parentheses to indicate which element of the array we are entering. The number within the parentheses is called the subscript. An element is another way of saying one piece of data within the array. Line 1040 will PRINT on your screen the information as you enter it. When you are finished, you will see your chart, numbered 1-10, with the data you have just entered. The final line in this short example is line 1060, which sends the computer back to line 1000 ten times, and increases the value of “F” by one each time. When the value of “F” is 10, the program continues past this point. The following example shows another method of filling an array on the T/S 2068, utilizing the READ and DATA statements. Replace line. 1020 in the above example with: 1020 READ A(F), and add this line: 9900 DATA 10, 20, 30, 40, 50, 60, 70, 80, 90, 100. In this case, instead of asking the user to INPUT the data, the program (line 1020) READs the DATA statement, inserting the 10 values in that statement into the 10 places in the array.

Although the Timex 1000 series does not have the READ or DATA statements, the same thing can be accomplished by taking advantage of the Sinclair Basic’s string slicing capabilities. By replacing the DATA statement with line 20 in the following example, you can simulate the DATA statement.

10 DIM A(10) 
20 LET Z$ = "10 20 30 40 50 60 70 80 90 100"
30 LET X=1
1000 FOR F = 1 TO 10
1020 LET A(F) = VAL Z$(X TO X+ 2)
1030 LET X =X + 3
1040 PRINT TAB 8,"A(";F;") = ";A(F)
1060 NEXT F

When doing this, it is important that you decide how many characters that your largest piece of data will contain, since each new piece of data must start at a fixed interval. In the example above, the largest number we want to enter requires three characters – 100. Therefore, each new entry must start at an interval of at least three characters. Line 30 sets the variable “X” to one, as this is the pointer which we will use to determine the start of each new bit of information. Line 1020 is the place where the values are assigned to their positions within the array. We use the VAL(ue) statement to do this, along with the string slicing. Since the data is in substrings of three characters each, we assign A(F) the value of Z$(x TO x + 2). In the first pass through the loop, x=1, therefore a (1) would equal the value of the first three characters of Z$. In the second pass through the loop, the value of x is increased by 3 making it have a value of 4. This time A(2) is assigned the value of Z$ starting with position 4 through position 6, which in the above example is 20. Note that only spaces may be used to fill in the necessary characters.

So far, what we have been discussing is what is known as a one dimensional array. This is just a chart with a single line. If we want to create a chart more than one line, we use a multi-dimensional array. Enter the following program to see how this would look.

10 DIM A(12,10)
20 PRINT"--1-2--3--4--5--6--7--8--9--10"
30 FOR F=1 TO 12
40 PRINT AT F+ 2,0;F
50 NEXT F
1000 FOR F=1 TO 12
1020 FOR G= 1 TO 10
1040 INPUT A (F,G)
1050 IF A(F,G)>99 THEN GO TO 1040
1060 PRINT AT F + 2,G*3;A(F,G);
1070 NEXT G
1080 NEXT F

Let’s now look at this pro- gram line by line. Line 10 again is the DIM(ension) statement, but as you see, it contains two numbers this time. In the first example, we created a chart that consisted of a single line across, containing ten entries. This time we will create a chart that has the same ten entries across, but also consists of 12 lines down. Although it does not matter which number is put first into the DIM statement, you have to be aware of which you have put in which position so that it may be used in the manner in which you intended. In this case the 12 of the array may be used to differentiate the entries by the twelve months of the year. You would then use the 10 portion of the array to keep track of ten entries for each month. Line 20 sets PRINTS column numbers at the top of the screen to show you how your entries are being entered in the array. Lines 30, 40, and 50 do the same for the rows from top to bottom. In this case if you were using it to make entries by month, these would represent the twelve months. Note that these numbers were selected to fit on your screen (32 characters across), but you are not restricted to what can fit in the screen, as the entries are kept in memory, and do not have to be PRINTed. Line 1000 sets up first FOR/NEXT loop, which will repeat 12 times. Line 1020 sets up second FOR/NEXT loop, which repeats 10 times. Since when we originally set up the array in line 10 with the 12 first, and the 10 second, the variable that we will be INPUTing becomes A(F,G). We therefore will be filling the array across, and then proceeding down to the next row. For our purposes we will keep our entries to a one or two digit number so that our chart will be clear, so line 1050 sees to it that no number greater than 99 is entered. If such a number does get entered, the program will revert back to line 1040, and request another value, which should be less than 100. Line 1060 PRINTs the entry into the proper place on our chart. Do not forget to put the semicolon at the end of this line. Line 1070 and 1080 conclude their respective FOR/NEXT loops. Be aware that the GO TO statement in line 1050 will ap- pear in two different manners depending on whether you are using a 1000 series computer, or a 2068. The 1000 series shows this command as GOTO (one word) where the T/S 2068 (and Spectrum and ZX-80) will display it as GO TO (two words). When referring to a member of this array, you will first read the row at the left, since this is the part of our ar- ray which has 12 elements, and the first number in our DIM statement is 12. The columns across the top will account for the second number, since our DIM statement has 10 as the second number, and there are 10 columns across. Therefore the number three from the left, and six from the top will be A(6,4).

Up to this point we have only been discussing numeric arrays, but there are also string, or character, arrays. In this type of array, you can enter anything that the computer can put on the screen. The difference in the DIM statement is that you must state the length of each of the elements of this type of array. Try this program and enter the follow- ing data when you are requested to make an entry: ANIMALS; SEASONS; and COLORS.

10 DIM AS(3,7) 
1000 FOR F = 1 TO 3
1020 INPUT AŞ(F)
1040 PRINT A$(F);" ";
1060 NEXT F

Let’s look at the DIM statement. First we see that instead of just “A” we have used “A$” which tells the computer that this is going to be an array which will contain any combination of characters, not necessarily a number. The first number within the parentheses tells the computer that it must set aside room for three elements within this array. The second number says that each element is to be seven characters long. If you make an entry that is less than seven characters, the computer will add enough spaces to bring the total to seven. If your entry is greater than seven characters, the computer will recognize only the first seven characters, and chop off the rest. The rest of this example is exactly the same as in the examples we used for numeric arrays. We can also work with multi-dimensional string arrays in the same manner as we do with numeric arrays. Again, of course, we must declare the length of the entries. Add this to the program above:

20 DIM B$ (3,5,7) 
2000 FOR F = 1 TO 3
2020 FOR G = 1 TO 5
2040 INPUT B$ (F,G)
2060 PRINT AT C+1,(F - 1)*8;B$(E,G)
2080 NEXT G
2090 NEXT F

The first two numbers in the DIM statement in line 20 declare the dimensions of the array, while, as in the single dimension array above, the final number states how many characters each element is to contain. This time RUN the entire program and for the first three requests for INPUT, enter three categories of your choice. These will appear on the screen, and will ask for further data. The next five entries should refer to the first category, the next five, the second, and the final five, the last. Were any of your entries longer than five characters? If so, what happened?

With this tool in your programming satchel, you should be able to write some very useful applications. Let me hear from you as to what you’ve done using these techniques. The next column will contain a few reviews as well as a list of some interesting sources of software and hardware for the T/S 2068. In the meantime, I will continue to watch my mailbox for your INPUT.

Products

 

Downloadable Media

 

Image Gallery

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

Scroll to Top