The following chart shows a two dimensional integer array; A(3,3). The number of elements are determined by the BASE OPTION that was configured when loading ZBasic. The default is Base 0:
A(3,3) BASE 0 dimensions are 4 elements down (0,1,2 and 3) and 4 elements across (0,1,2 and 3). Base zero utilizes all the elements including the italicized.
A(3,3) BASE 1 dimensions are 3 elements down (1,2,3) and 3 elements across (1,2,3) (not the italicized):
TWO DIMENSION ARRAY
A(O,O) A(t,O) A(2,O) A(3,O) A(O,t) A (1,1) A (2 ,1) A(3,l) A(O,2) A(l,2) A(2,2) A(3,2)
A(O,3) A (1, 3) A (2,3) A (3,3)
---This array was DIM(med) A(3,3). A(1,3) represents the cell underlined above. Accessing a cell only requires giving the correct coordinate after the variable name.
Variables, constants or expressions may be used in specifying coordinates:
A(3,2), A(X,Y), A(2,X) , A(X*2/3,2+Y) .
BASE OPTION
Zero is considered an element unless you set the BASE OPTION to one when
configuring ZBasic. See "Configure" for more information about setting the Base option.
The default BASE is zero.
DEFINING THE DIMENSIONS OF AN ARRAY
73
A"ay VariablesAll variable arrays ~ be DIMensioned at the beginning of a program. When you RUN a program, memory is set aside for the array based on the number of elements you have DIMensioned.
An example of DIM:
DIM A%(10,10,10), MI(5), A! (9,7), B$(10), 5Coo1$(20) Only numbers may be used within DIM statement parentheses. The following DIM expressions are /IIegal :
DIM A(X), A(2*X) , A(FR) •
ARRAY VARIABLES
~" ,~ " .
~ . ) . . '
.
~ , " , " , \ ,HOW ARRAYS USE MEMORY
The following chart shows how to calculate the memory requirements of the arrays DIMensioned above with a BASE OPTION of zero (default value).
Bytes per How to Memory
ARRAY
IY.M
Element Cal!;ulat~" Reg!,!lregA%(10,10,10) INTEGER 2 11'11'11'2 2662 Bytes
A#(5) DOUBLE PREC. 8 6'8 48 Bytes
Ai (9,7) SINGLE PREC. 4 10*8*4 320 Bytes
B$(10) STRING 256 11'256 2816 Bytes
Cool$(20) STRING 6 21*6 126
"Note: If you use a BASE OPTION of ONE, you will not need to add one to the dimension. For instance, in the first example the way to calculate the memory required would be: 10*10*10*2. Also see DEF LEN and DIM under STRING VARIABLES for info about defining string lengths.
Macintosh also has Longlnteger arrays. Each element takes 4 bytes.
ARRAY BOUNDS CHECKING
During the initial stages of writing a program, it is a good idea to configure ZBasic to check array bounds in runtime. See "Configure" for more information.
OUT OF MEMORY ERROR FROM DIMMING
It is necessary to have an understanding of how arrays use memory. DIMensioning an array larger than available memory will cause ZBasic to give an OUT OF MEMORY error at Compile time or RUN time. When calculating large arrays be sure to check if memory is sufficient.
Array Variables
74
ARRAY VARIABLES
PRINTING ARRAYS
Arrays were designed to make manipulating large lists of data easy. The following routines print the values of ARRAY(50) and/or ARRAY(50,5) to the screen (Substitute LPRINT for PRINT or use ROUTE 126 to print to the printer). Use AUTO or make your own line numbers. It does not matter which numbers are used.
"One Dimension array PRINT routine"
DIM ARRAY (50) FOR X=0 TO 50
PRINT ARRAY(X) NEXT
"Two Dimension array PRINT routine"
DIM ARRAY (50, 5) FOR X=0 TO 50
FOR X2=0 TO 5 PRINT ARRAY(X,X2), NEXT X2
PRINT NEXT X
MAKING AN ENTIRE ARRAY ONE VALUE
75
Affay VariablesThe following examples show how to make an entire array (ARRAY(50) or ARRAY(50,5)) equal to a certain value. This would be convenient if you wanted to zero out an array or have all the elements start the same values.
"One Dimension array ASSIGNMENT routine"
DIM ARRAY (50) FOR X=0 TO 50
ARRAY(X)=VALUE NEXT
"Two Dimension array ASSIGNMENT routine"
DIM ARRAY (50, 5) FOR X=0 TO 50
FOR X2=0 TO 5 ARRAY(X,X2)=VALUE NEXT X2
NEXT X
ARRAY VARIABLES
USING ARRAYS FOR SORTING
Arrays are also very convenient for organizing large lists of data alphabetically or numerically, in ascending or descending order.
The first program below creates random data to sort. This program is for example purposes only and should not be included in your programs. These programs are Included on your master disk.
Follow the GOSUB with the label of the sort routine you wish to use (either "QUICK SORT" or "SHELL SORT"). Any line numbers may be used. These sort routines may be copied and saved to disk (using SAVE' or +) as a subroutine to be loaded with APPEND.
See APPEND.
""S""OJ,JR .... T ... ,B""A"S'-_ _ FILL ARRAY WITH RANDOM DATA FOR SORTING _ _ DIM SA(500), ST(30,1): REM ST (30,1) FOR QUICK SORT ONLY, NI=500: REM Change DIM 500 and NI if sort larger FOR X=OTO NI
SA(X)=RND(1000) : REM Stores random numbers for sorting NEXT
PRINT"Start Time:";TIME$
GOSUB "QUICK SORT": REM Or SHELL SORT PRINT"Finish Time:";TIME$
FOR X=NI-10 TO NI
PRINT SA (X): REM Print last to make sure SORT worked.
NEXT END
>i!S!lH!:!EL!:!L"".A~PC1P=--_ _ _ _ _ SHELL-METZNER SORT _ _ _ _
"SHELL SORT" Y=NI
"Zl" Y=Y/2
IF Y=0 THEN RETURN: REM Sort complete Z99=NI-Y
FOR K9=1 TO Z99 I=K9
"X2" E2=I+Y
REM: In line below change <= to >= for descending order IF SA ( I ) <= SA (E2) THEN "x3" ELSE SWAP SA ( I ), SA (E2) I=I-Y
IF 1>0 THEN "X2"
"x3" NEXT K9 GOTO "Zl"
END
Note: To sort string arrays instead of numeric arrays add a "$" to the appropriate variables.
Also see "Perpetual Sort" using INDEX$ in the previous chapter.
Array Variables
76
ARRAY VARIABLES
77
Array Variables~au~I~OUW~uP
___________
auCK~____________ _
"QUICK SORT"
REM Improved Quicksort submitted by Johan Brouwer, Luxembourg.
REM Thanks for the submission, Johan.
Note: To use the QUICK SORT or SHELL SORT with STRING variables, use DEFSTR with the appropriate variables on the first line of the program or put a "$" after all variables that are strings
Be sure to use DEFLEN or DIM to define the length of the string variables. If each element needs 50 characters, then set the length of SA$ to 50. The default is 256 bytes per element for string variables if you do not define the length.
HINTS ON TYPING IN THE PROGRAM: First of all, use line numbers of your own chosing.
Indentation in this program is the way ZBasic shows the loops or repetitive parts of the program. You do not need to type in spaces (Make everything flush left). ZBasic will indent the listing automatically when you type UST or LUST.
Also see "Perpetual Sort" using INDEX$ in the previous chapter.
ARRAY VARIABLES
, • , • ~"~ • " ' ~f • ~. ;