ARRAYS› ======›› by Ron Fetzer›› There are 3 kinds of arrays that› we use in TURBO-BASIC or ATARI BASIC:› 1)One dimensional number array› 2)Two dimensional number array› or MATRIX› 3)String Arrays(Pseudo-String› Array)›› Arrays are also called› Subscripted Variables. P(2) = "P sub› 2". The P is the variable and the 2› is the subscript. The subscript is› always in parenthesis P(3), P(4)› etc.›› Arrays are very useful in› manipulating numbers and strings. It› is the ONLY way you can sort numbers› or alphabetize strings. You also save› on variable names, you can save› numbers or words in a program, you› can handle large amounts of numbers› or strings and you can speed up your› program execution. This is a› very useful technique and is› frequently used in many programs.›› ONE DIMENSIONAL ARRAYS› ----------------------›› You can think of a ONE› DIMENSIONAL ARRAY as a string of› pearls. Each pearl is an element of› the array.›› PEARL NECKLACE›› -O----O----O----O----O----O----O-›› Element #› 0 1 2 3 4 5 6› Value $› $25 $33 $42 $50 $38 $28 $22››› In number arrays the 1st element› is 0. In the example above we have 7 › elments from 0 to 6. When we use an› array we must DIM it. The DIM also› starts with 0, in effect we› dimensioned for 7 elements or pearls› if we use DIM P(6). The form of an› array is:›› P(X) = V›› P = The name of the array. You must› DIM it for the number of› elements you are going to have› in your array.›› X = The element numbers. Usually› a FOR - NEXT LOOP›› V = Inputs the value into each› element as the loop executes›› ELEMENTS AND THEIR VALUE›› P(0)=25, P(1)=33, P(2)=42, P(3)=50,› P(4)=38, P(5)=28, P(6)=22›› Please key in the short demo› program to see how it works›› 10 CLS:?:?› 20 DIM P(6)› 30 FOR X = 0 TO 6› 40 INPUT "THE VALUE OF EACH"› PEARL";V› 50 P(X)=V:REM <-- ARRAY› 60 NEXT X› 70 PAUSE 60› 80 ? "LETS SEE IF IT WORKED"› 90 ?:INPUT "WHAT PEARL DO YOU› WANT TO SEE(0-6)";S› 100 ? P(S):REM <-- ARRAY› 110 GOTO 90›› You now can call up any pearl or› element in the array and see its› value.›› You also can print out all the› values of the pearls or elements› automatically. Change lines 90 to 110› as follows:›› 90 ?: FOR Y = 0 TO 6› 100 ? "PEARL #";Y;" = ";P(Y):› REM P(Y) IS THE ARRAY› 110 NEXT Y››› 2 DIMENSIONAL ARRAY OR MATRIX› -----------------------------›› 2 Dimensional arrays or Double› Subscripted Variables are also called› a MATRIX. A matrix is a grid where› you have columns and rows.› For example lets take a bowling› team of 3 members and each one plays› 4 games. This is how you can› visualize the matrix:›› BOWLERS›› 1 2 3 › ---- ---- ----› B(0,0)|250|B(0,1)|220|B(0,2)|240|› G ---- ---- ----› ---- ---- ----› A B(1,0)|210|B(1,1)|190|B(1,2)|250|› ---- ---- ----› M ---- ---- ----› B(2,0)|240|B(2,1)|230|B(2.2)|270|› E ---- ---- ----› ---- ---- ----› S B(3,0)|220|B(3,1)|200|B(3,2)|260|› ---- ---- ----› In a matrix the first element› also starts with 0. In the example› above we have a matrix and we must› DIM it DIM(2,3). The 2 in the DIM is› the 3 bowlers (0 to 2). The 3 in the› DIM is the 4 games (0 to 3). We have› a total of 3 x 4 or 12 elements in› this 2 dimensional array.›› The bowling score for the 4› games were as follows:›› BOWLER 1 BOWLER 2 BOWLER 3› -------- -------- --------›› 250 220 240› 210 190 250› 240 230 270› 220 200 260›› In the matrix the first element› is (0,0). We normally do not consider› 0 a number but the computer does not› make that distinction.›› Please key in this short demo› program to see how it works›› 10 CLS:?:?› 20 DIM B(2,3)› 30 FOR X=0 TO 2› 40 FOR Y=0 TO 3› 50 ? "BOWLER ";X+1;" GAME #";› Y+1› 60 INPUT "SCORE PLEASE ";S› 70 B(X,Y)=S:REM <--MATRIX› 80 ?› 90 NEXT Y› 100 NEXT X› 110 ?:PAUSE 60› 120 ? "LETS SEE IF IT WORKED"› 130 INPUT "WHAT BOWLER(1-3)";P› 140 INPUT "WHAT GAME(1-4)";G› 150 ? " ";B(P-1,G-1):REM› MATRIX› 160 GOTO 130›› We use nested loops to load the› matrix. The first element to get› loaded is B(0,0). In line 50 we use› X+1 for the bowler - if the loop is 0› then the bowler becomes 0+1 or 1. We› did the same thing for the games Y+1› so when the loop furnishes a 0 is› becomes 0+1 or 1. We did the same› thing in line 150 except in reverse› so the matrix will find the right› element. When you answer for the› bowler 1 it becomes P-1 or 1-1 or 0› so the right element is accessed. The› same is done for the game G-1 becomes› 1-1 or 0.›› ELEMENTS AND THEIR VALUES›› B(0,0)=250 B(0,1)=220 B(0,2)=240› B(1,0)=210 B(1,1)=190 B(1,2)=250› B(2,0)=240 B(2,1)=230 B(2,2)=270› B(3,0)=220 B(3,1)=200 B(3,2)=260›› You also can print out all the› bowling scores or elements› automatically. Please change lines› 130 to 180 as follows:›› 130 FOR T=0 TO 2› 140 FOR K=0 TO 3› 150 ? "BOWLER ";T+1;" GAME #";› K+1;› 160 ? " = ";B(T,K):REM<--MATRIX› 170 NEXT K› 180 NEXT T›› As a practical matter most› programmers do no use element 0› either in an array or a matrix› because it complicates the program.› DIM the array or matrix for the› number of elements you have. The FOR› - NEXT loop starts with 1 instead of› 0. See the program below.›› 10 --› 20 CLS:?:?› 30 DIM B(3,4)› 40 FOR X = 1 TO 3› 50 FOR Y = 1 TO 4› 60 ? "BOWLER ";X;" GAME #";Y› 70 INPUT "SCORE PLEASE ";S› 80 B(X,Y)=S:REM <--MATRIX› 90 ?› 100 NEXT Y› 110 NEXT X› 120 PAUSE 60› 130 --› 140 ? "LET'S SEE IF IT WORKED?"› 150 FOR T = 1 TO 3› 160 FOR K = 1 TO 4› 170 ? "BOWLER ";T;" GAME #";K;› 180 ?" = ";B(T,K):REM<--MATRIX› 190 NEXT K› 200 NEXT T››› STRING ARRAYS› -------------›› TURBO-BASIC and ATARI BASIC do› not allow string arrays. This is no› big problem. We create a PSEUDO› STRING ARRAY that looks and acts like› a normal string array. We take a long› string and chop it into equal pieces.› In order to create this PSEUDO STRING› ARRAY we must do certain things.›› 1)Decide how many elements we› need› 2)The length of each element› 3)Calculate the end of the› FOR-NEXT loop and the STEP›› For example, we want to create a› string array that has 7 elements and› each element is 10 letters long. We› will call the main string X$. To find› out how much to DIM X$ we multiply› the number of elements times the› length of each element (7 x 10 = 70).› We now DIM X$(70). The FOR-NEXT loop› starts with 1 and ends with the DIM› in this case 70. The step is the› element length(10) 'FOR N = 1 TO 70› STEP 10›› Since the words we put into the› string array will be shorter than 10› letters, we use PAD$ which has 10› empty spaces to pad each substring or› element so it is 10 letters long. We› can visualize this as follows:›› X$›› -----------------------------------› | | | | | | | |› |EL 1|EL 2|EL 3|EL 4|EL 5|EL 6|EL7|› | | | | | | | |› -----------------------------------› 1 10 20 30 40 50 60 70›› Elements or substrings start and› end as follows:›› ELEMENTS OR SUBSTRINGS›› Element 1 = 1 to 10› Element 2 = 11 to 20› Element 3 = 21 to 30› Element 4 = 31 to 40› Element 5 = 41 to 50› Element 6 = 51 to 60› Element 7 = 61 to 70›› It looks as if each element is› only 9 letters long. These numbers› are inclusive so each one is 10› letters long›› If we have a FOR-NEXT-STEP loop› we can generate these numbers.›› 10 FOR N = 1 TO 70 STEP 10› 20 ? N, N+9› 30 NEXT N›› The result of this loop is as› follows:›› 1 10› 11 20› 21 30› 31 40› 41 50› 51 60› 61 70›› This is the way the program› would look. Please key in this short› demo program.›› 10 --› 20 CLS:?:?› 30 DIM X$(70),SUB$(10),PAD$(10)› 40 PAD$=" ":REM FOR› PADDING THE SUBSTRING 10› SPACES› 50 REM ASSIGNING PSEUDO STRING› ARRAY VALUES› 60 FOR N = 1 TO 70 STEP 10› 70 INPUT "GIVE ME A WORD(MAX› 10 LETTRS)";SUB$:?› 80 SL=LEN(SUB$):REM LENGTH OF› WORD› 90 IF SL<10 THEN› SUB$(SL+1)=PAD$:REM PAD TO› 10 LETTERS› 100 X$(N,N+9)=SUB$:REM PUT› VALUE INTO ELEMENTS› 110 NEXT N› 120 --› 130 ?:?› 140 REM DISPLAY PSEUDO STRING › ARRAY› 150 FOR N=1 TO 70 STEP 10› 160 K=K+1:REM COUNTER› 170 ? "ELEMENT ";K;" = ";› X$(N,N+9)› 180 NEXT N› 190 --›› A line by line explanation of› this program is as follows:›› 10 Draw 30 dashes› 20 Clear the screen› 30 Dim X$ for the total amount of› all the elements. The SUB$ is› the length of an element. The› PAD$ is the length of an element› 40 PAD$ = is the length of an› element(10). It is used to pad› a word that is too short - less› than 10 letters› 50 Rem explanation› 60 FOR-NEXT-loop. It starts with 1› and ends with the number of› elements(7) times the length of› each element(10) or (7x10=70).The› step is the length of the› element(10)› 70 INPUT the words› 80 Find out how long each word is.› 90 Pad each word to 10 characters› 100 Fill the elements or the SUB$› with words. The sub strings or› elements are 1-10, 11-20, 21-30,› 31-40, 41-50, 51-60, 61-70› 110 End of loop› 120 Draw 30 dashes› 130 Print› 140 Rem explanation› 150 FOR-NEXT loop. See explanation› on line 60› 160 A counter that counts the number› of elements in the array› 170 Print the elements in X$(N,N+9)› The substrings or elements in X$› are 1-10, 11-20, 21-30, 31-40,› 41-50, 51-60, 61-70› 180 End of loop› 190 Draw 30 dashes›› You can also print out› individual elements of the string› array. Please change the following› lines.›› 150 N = 0› 160 INPUT "WHAT ELEMENT DO YOU› WANT TO SEE";N› 170 ? "ELEMENT ";N;" = ";› X$(N*10-9,N*10)› 180 GOTO 150›› >>>>>>>>>>>>>>> END <<<<<<<<<<<<<<<<››