
Five players' scores in five variables is bearable. Fifty is not, and twenty-five platforms in a game is impossible that way - not because it is long to type, but because you cannot write a loop over variables with different names.
An array is one name with a number after it, and that number can be worked out while the program runs. It is what turns a handful of values into something a loop can sweep through.
10 DIM PTS(5)
makes an array called PTS with room for numbers. After that, PTS(1), PTS(2) and so on are each an ordinary numeric variable - you can put numbers in them, print them, add them up.
The point is that the number in the brackets can be a variable:
20 FOR I=1 TO 5
30 PTS(I)=I*I*10
40 NEXT I
Five different places, filled by one line.
DIM PTS(5) does not give you five numbers. It gives you six: PTS(0) to PTS(5).
That is not a mistake in the manual, it is how BASIC counts, and it catches everybody once. You have two honest choices: ignore PTS(0) and use 1 to 5 as this section does, or use 0 to 4 and remember that the last one is one less than the size. Either is fine. Mixing them is not.
An element you have not filled in is 0 - or empty, for a string array - just like an ordinary variable.
You can use an array without DIMing it at all:
10 B(3)=7
20 PRINT B(3);B(10)
works, and prints 7 0. BASIC quietly makes B with subscripts 0 to 10 the first time you touch it.
That is convenient and it is a trap: ask for B(11) and you get a Range Error, because the array is only as big as BASIC guessed. DIM what you are going to use, and the error goes away.
You cannot change your mind afterwards: a second DIM of the same array is a Dimension Error.
An array can have two numbers, which makes it a table with rows and columns:
10 DIM GRID(2,3)
20 GRID(1,2)=5
GRID(1,2) is row 1, column 2 - or column 1, row 2, whichever way round you decide, as long as you stay consistent. It is the natural shape for a noughts-and-crosses board, a map, or a screenful of tiles.
Both subscripts start at 0, as before.
10 DIM NAME$(3)
20 NAME$(1)="CAT"
A $ on the name makes every element a string. Unset elements are empty.
10 DIM PTS(5)
20 FOR I=1 TO 5
30 PTS(I)=I*I*10
40 NEXT I
50 FOR I=1 TO 5
60 PRINT "PLAYER";I;"SCORED";PTS(I)
70 NEXT I
80 T=0
90 FOR I=1 TO 5
100 T=T+PTS(I)
110 NEXT I
120 PRINT "TOTAL";T
Starting from
A fresh boot, or NEW.
What you should see
RUN
PLAYER 1 SCORED 10
PLAYER 2 SCORED 40
PLAYER 3 SCORED 90
PLAYER 4 SCORED 160
PLAYER 5 SCORED 250
TOTAL 550
Ready

Three loops over the same array: one to fill it, one to print it, one to add it up. None of them mentions a particular player, and none would change if there were fifty.
DIM PTS(50) and lines 20 and 50 to FOR I=1 TO 50. What else did you have to change? What does that tell you about why arrays are worth the bother?130 PRINT PTS(0) and run it. Where did that value come from, and why is it not part of the total?14.1 Fill an array with the numbers 1 to 10, then print them backwards without changing the array.
14.2 Ask for five numbers with INPUT, store them, and then print the largest. (Keep a variable holding the biggest so far and compare each one against it.)
14.3 Make a two-dimensional array 3 by 3, fill it so that each element is its row times its column, and print it as a square - three numbers to a line.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
Range Error in <line> | A subscript past the end of the array. Remember an un-DIMmed array only goes up to 10. |
Dimension Error in <line> | The array has been DIMmed twice - often a DIM inside a loop, which runs a second time. |
Syntax Error on a DIM line that looks perfect | A keyword inside the array's name - SCORES is SC OR ES. See S8, and test the name. |
| One element too many, or one too few | DIM A(5) gives A(0) to A(5): six. |
Mem Full Error | The array is bigger than the memory left. PRINT SIZE before and after to see what it costs. |
The array is empty after a RUN | RUN clears arrays as well as variables, every time. |
DIM name(n) makes an array of n+1 elements, numbered from 0. The subscript can be a variable, which is the whole point - a loop can sweep the lot. An array used without DIM is made with room for 0 to 10. Two subscripts make a table. A $ makes it hold text.
S15, Ready-Made Data - putting a list of values into the program itself, rather than typing them in every time it runs.