13/ARRAYS
Even though you can use the 26 letters of the alphabet as variables, there may be times when you wish to have more variables available-sometimes hundreds of them-to use as names for different pieces of data. For instance, a car rental agency has a list of automobiles, all of which are the same make and model, but are distinguished by different license plate numbers and colors. Micro Color BASIC uses "arrays" to differentiate between these pieces of data.
Eight cars may be lined up, as in an array. They are all the same except for their license numbers, which vary from 100 to 800, and for the color, which is different. In this case, you can give each of the cars a different letter name, using the variables A through H. But what if the agency had 8,000 cars instead of 8!
Micro Color BASIC provides for a single array which we'll call "A". This isn't the same as the alphabet letter A or the string variable A$. It is a third and totally separate "A". You will recognize it A-sub (something). In other words, you can name the cars as A(1) through A(8), pronounced A sub 1 through a sub 8.
To check this out, type this in (using the Immediate Mode):
A = 12 [ENTER]
A$ = "MC-10" [ENTER]
A(1) = 999 [ENTER]
Now type:
PRINT A; A$; A(1) [ENTER]
and you TV will display:
12 MC-10 999
OK
One-Dimensional Arrays
A one-dimensional array is like a numbered list.
For instance, store the license numbers of the rental agency's cars in the following DATA line:
100 DATA 100,200,300,400,500,600,700,800
Now you must create a one-dimensional array to make the data items immediately addressable which is the easiest way to access a specific item in the list. Type in this part of the program:
50 FOR L = 1 TO 8 :REM L IS THE LICENSE NUMBER
60 READ A(L)
70 NEXT L
RUN the program at this point. Nothing happens except the OK prompt returns. This is because the MC-10 hasn't been told to print the results. In the Immediate Mode, type this:
PRINT A(1) [ENTER]
and the TV will display 100.
Then type:
PRINT A(5) [ENTER]
and the Screen will display 500.
The values have been read into the array; you just told the Computer to display the results. You could add to your program eight lines such as:
200 PRINT A(1)
201 PRINT A(2)
202 PRINT A(3)
203 PRINT A(4)
204 PRINT A(5)
206 PRINT A(6)
207 PRINT A(7)
208 PRINT A(8)
but this can be done much more efficiently by creating another loop:
200 FOR N = 1 TO 8
210 PRINT A(N)
220 NEXT N
Add these three lines and LIST your program to be sure it looks like this:
50 FOR L = 1 TO 8 :REM L IS THE LICENSE NUMBER
60 READ A(L)
70 NEXT L
100 DATA 100,200,300,400,500,600,700,800
200 FOR N = 1 TO 8 :REM N IS THE NUMBER OF CARS
210 PRINT A(N)
220 NEXT N
Now RUN it. Your TV will display:
100
200
300
400
500
600
700
800
What happened is the DATA elements were read into the array called A(L), but printed out an array called A(N). The array's name is A. The location of each data element within that array is identified by the number which is placed inside the parentheses.
Remember, though, there is only one array, and its name is A. Its elements are numbered, and called A-sub(number).
Try refining the program a little. Type:
170 PRINT
180 PRINT "LICENSE #"
It's also possible to include the colors for the cars. For convenience, assign each color a code. (To make it easy, use the same color codes that the CLS statement recognizes.):
Code |
For This Color |
0 |
black |
1 |
green |
2 |
yellow |
3 |
blue |
4 |
red |
5 |
buff |
6 |
cyan |
7 |
magenta |
8 |
orange |
Now modify your program in this manner:
100 PRINT "LICENSE #","COLOR CODE"
210 PRINT A(N),N
Now you have every license number and color code and you're still not using any of the 26th alphabetic variables.
To make the program even more powerful, erase lines 200, 210, and 220 and type:
10 INPUT "WHICH CAR'S LICENSE # DO YOU WISH TO
KNOW";C :REM TYPE IN THE COLOR CODE
210 PRINT A(C),C
and RUN the program.
The DIM Statement
In most cases, you can use arrays as we've described; but if your array items (that information inside parentheses) are greater than 10, you'll probably want to use the DIM statement. This command reserves just the right amount of memory needed for your array and, with the MC-10, it's important to be conscious of the memory your program requires. To create an array using DIM:
DIM variable (row, column) variable is the name of the array row and column tells what the largest index or "subscript" will be. row and column can be numeric variable, constant, or expression. row without column specifies a one-dimensional array; row with column specifies a two-dimensional array. |
Once an array has been created with DIM, you can access any item in it by putting the appropriate subscript inside the ( ).
To show you how the DIM statement works, we're going to make your car rental agency program even more powerful. When a customer wants to rent a car, you might need to know three things: the color, the license number, and if it is available to be rented. The first two of these have been discussed already. In this section, we'll cover the availability for rental. In the following program, "0" will mean the car is not available for rental; "1" will mean that it can be rented.
Modify your program so it looks like this:
10 DIM A(8),B(8)
20 DATA 100,200,300,400,500,600,700,800
30 DATA 1,1,0,0,1,0,1,0
40 FOR L = 1 TO 8 :REM L IS THE LICENSE #
50 READ A(L)
60 NEXT L
70 FOR R = 1 TO 8 :REM R IS AVAILABILITY FOR
RENTAL
80 READ B(R)
90 NEXT R
100 INPUT "WHAT COLOR OF CAR DO YOU WANT";C :REM
INPUT A COLOR CODE
110 PRINT "LICENSE # "; "COLOR "; "RENTED?"
120 PRINT A(C);" ";C;" ";B(C)
130 END
In this program, line 10 reserves space for two arrays. If the DATA statements in line 20 and 30 contain more items than allowed for, an error will occur. You'll find it convenient to put the DIM statements early in the program, certainly before the loops start in line 40. Otherwise a ?DD ERROR will occur.
The loop in lines 40-60 reads array A data. The loop in lines 70-90 reads array B data.
The most difficult part of this program for you might be printing out the results. Line 120 does this. Notice that the current value of the variable C is used to define the specified array element in both A and B.
As a car is rented or returned, the rental agent would simply have to go and change the rental status in line 30.
In the language of professional computer types, this is called a matrix.
Two-Dimensional Arrays
For programs that have more data, you might find it more efficient to use two-dimensional arrays by specifying the column option. Take a look at the following program to see how DIM is used in this manner:
10 DIM G(12,8)
20 FOR M = 1 TO 12
30 FOR I = 1 TO 8
40 G(M,I)=RND(10) :REM NUMBER RENTED IS RANDOM
50 NEXT I,M
60 INPUT "WHAT MONTH DO YOU NEED INFORMATION ON";M
:REM TYPE A NUMBER FROM 1 TO 12
70 INPUT "WHAT COLOR OF CAR";C :REM TYPE A NUMBER
FROM 1 TO 8
80 PRINT "YOU RENTED ";G(M,C); "IN MONTH"; M
90 END
This program would supply the rental agency with the number of cars of a certain color that was rented during the specified month (1-12, January-December) of the previous year. Note that in this case, the sample data is "random", supplied by the RND statement. (For a complete description of RND, see the last part of this chapter.) In a real life situation, this might be data that you actually typed into the Computer-data that was processed by the Computer.
Using DIM G(12,8) actually creates a table containing 12 columns and eight rows. If you consider how many entries there are in a 12x8 table, you can see that two-dimensional arrays offer much more storage space than a one-dimensional array.
The CSAVE* Command
The contents of a numeric array can be saved to tape as a separate file. Then, when you need to use that data, you can load it back into the MC-10's memory. CSAVE* is used like this:
CSAVE* array name, filename array name is the name you previously assigned to an array. filename is a standard Micro Color BASIC filename of eight or fewer characters. filename is optional; if omitted, a filename is not assigned. |
The specified array must be previously defined (either explicitly by execution of a DIM statement, or implicitly by the assignment of a value to one of the elements 0 through 10).
If filename is not specified, a name is not assigned to the file on tape. For this reason, it is recommended that a filename always be specified when using CSAVE*.
For more details, see CLOAD*.
The CLOAD* Command
Once the contents of a numeric array has been saved to tape with CSAVE*, you can retrieve that data by using the CLOAD* command
CLOAD* array name, filename array name is the name you previously assigned to an array. filename is a standard Micro Color BASIC filename of eight or fewer characters. filename is optional; if omitted, a filename is not assigned. |
The specified array must be previously defined (either explicitly by execution of a DIM statement, or implicitly by the assignment of a value to one of the elements 0 through 10).
If the specified array is not large enough to hold all the data contained in the cassette file, an OM (Out of Memory) error will result.
If the specified array is larger than necessary to hold the data contained in the cassette file, the data in the file will be loaded starting at the beginning of the array. The values of the extra elements of the array will be unaffected.
For example, if CLOAD* is used to load data into an array of 20 elements from a file which was created by CSAVE* using an array with 10 elements, the first ten elements of the array would contain the data read from the file. The last ten elements would contain the same values they had before CLOAD* was executed.
If a filename is not specified, Micro Color BASIC will load the first file it encounters on the tape.
If an attempt is made to CLOAD* a file of wrong type (one not created with CSAVE*), an FM (File Mode) error will result.
Example
CLOAD* A,"DATAFILE"
The RND Statement
You've already seen how RND can be used in a program. Basically, RND generates pseudo-random numbers from zero to a specified limit.
RND (limit) limit is the greatest random number which can be generated and is a numeric expression. |
Try this example (in the Immediate Mode):
N = RND (100) [ENTER]
PRINT N [ENTER]
RND can also be used with variables:
10 A = 1000
20 X = RND(A)
30 PRINT X
40 END
Her's a short program that selects random numbers of either 1 or 2 and uses those numbers to simulate flipping a coin.
10 X = RND(2)
20 INPUT "CALL IT - HEADS (PRESS 1) OR
TAILS (PRESS 2)"; COIN
30 IF COIN <> X THEN GOSUB 100
40 IF COIN = X THEN GOSUB 200
50 GOTO 10
100 PRINT "YOU LOSE."
110 RETURN
200 PRINT "YOU WIN."
210 RETURN
Notice that you'll have to press [BREAK] to get out of this endless loop.
There Has To Be An Easier Way...
To enter RND without typing in the entire word, type:
[CONTROL][V]
|
|