RISCOS.com

www.riscos.com Technical Support:
BBC BASIC Reference Manual

 


Arrays


Arrays are groups of variables. An array has a name which applies to all variables in the group. The individual members, known as the elements of the array, are identified by a subscript. This is a whole number (zero or greater) indicating the element's position within the array. For example, A(0) is the first element in the array named A(), and A(1) is the second element, and so on.

The DIM statement

The DIM statement tells BASIC how many elements you wish to use in the array. For example,

DIM A(9)

allocates space in the computer's memory for ten elements, each called A(), but each having a different subscript, zero to nine. The DIM statement also assigns the value zero to each of these elements, which may then be individually assigned values, just like any other variables. For example:

A(1) = 0.56
A(2) = A(1) + 4

The example shown above is of a one-dimensional array: it may be thought of as a line of variables, numbered from 0 to 9 in a sequence. More dimensions may be used.

Two dimensional arrays

Two dimensional arrays in which the individual variables are identified by two subscripts can be thought of as the printing on a TV screen. Each character printed on the screen is at a particular position from the left, and a particular position from the top. (Use the rows and columns as a matrix.)

A two dimensional array may be defined as follows:

DIM B(2,2)

This allocates space for nine elements, each called B() in this case, and each identified by two subscripts as shown in the following table:

B(0,0) B(0,1) B(0,2)
B(1,0) B(1,1) B(1,2)
B(2,0) B(2,1) B(2,2)

Arrays may have as many dimensions as you like, and may hold floating point numbers, integers, or strings. For example,

DIM str$(1,3,2)

allocates space for 24 string variables (str$(0,0,0) to str$(1,3,2)), each of them containing up to 255 characters.

The subscript need not be specified as a number - a variable or expression can be used instead. For example:

10 DIM A(9)
20 X = 6
30 A(X) = 3
40 A(A(X)) = 1

This gives A(6) the value 3, and A(3) the value 1.

Any arithmetic expression may be used as a subscript. Since subscripts can only be whole numbers, any expression giving a floating point result has the number truncated to its integer value (the part before the decimal point).

When using arrays, remember that if you DIM the array using a particular number of subscripts, each element of the array must be referenced with the same number of subscripts:

10 DIM name$(2,2,2)
20 name$(0) = "FRED"

produces an error. Line 20 should be replaced by:

20 name$(0,0,0) = "FRED"

In addition, the numbers used as subscripts must not be too big or less than zero:

10 DIM position(9,4)
20 position(-1,5) = 1

If you now type RUN, an error message is displayed because the first subscript must be between zero and nine and the second between zero and four.

When you DIM a string array, the elements are initialised, just as they are for numeric arrays. Each element in the array is set to the null string, "". No space is allocated for the characters of each string element until they are assigned a value.

The operators += and -= are particularly useful with arrays, as they remove the need to evaluate the subscript expressions twice. For example, suppose you had the assignment:

a(100*(SINRADangle+1))=a(100*(SINRADangle+1))+increment

The expression 100*(SINRADangle+1) must be calculated twice, which could be quite time-consuming. On the other hand, if you used

a(100*(SINRADangle+1)) += increment

the complex subscript expression would only be used once, saving time. It is also easier to write and read!

Finding the size of an array

Functions are available to find the number of dimensions of an array, and the size of each dimension. To find the number of dimensions of an array type

PRINT DIM(A())

To find the number of elements of the nth dimension, type

PRINT DIM(A(),n)

For example,

10 DIM A(4,2,7)
20 n = DIM(A())
30 PRINT n
40 PRINT DIM(A(),n)

produces:

         3
         7

These functions are useful mainly in procedures and functions which take array parameters. See the chapter entitled Procedures for more details.

Operating on whole arrays

As described above, every element of an array is given the value zero when the array is DIMmed.

It is possible to set every element in an array to any given value using a single assignment as follows:

10 DIM A(10), B(10)
20 n% = 2
30 A() = (3*n%)
40 B() = A()

Line 10 dimensions two arrays of the same size. Line 30 sets all of the elements of A() to 3*n%, i.e. 6. Then line 40 sets all of the elements of B() from the corresponding elements in A().

Note: You may be wondering why the righthand side of the assignment in line 30 is in brackets, i.e. why couldn't we have written

20 A() = 3*n%

The answer is that the righthand side of an array assignment must be a single item (number, single variable or expression in brackets) to avoid possible confusion with a more complex array operation, for example

20 A() = 3*n%()

as described below.

Instead of setting all of the elements of an array to one value, you can set them to different values by giving a list of values after the =. For example:

10 DIM a(5), b(2,2)
20 a() = 1,2,3,4
30 b() = 6,5,4,3,2,1

Any elements omitted from the list are not changed in the array (for example, a(4) and a(5) above wouldn't be assigned). In the case of multi-dimensional arrays, the elements are assigned so that the last subscript changes quickest. For example, in the case of b() above the six values listed would be assigned to b(0,0), b(0,1), b(0,2), b(1,0),... , b(2,1), b(2,2) respectively.

In addition, all the elements in an array can be increased, decreased, multiplied or divided by a given amount:

10 DIM A(2,2), B(2,2)
20 A(0,0) = 4
30 A(1,1) = 5
40 A(2,2) = 650 n% = 2: m% = 3
60 A() = A() + (n%*n%)
70 A() = A() - m%
80 B() = A() * 6
90 B() = B() / n%

When you RUN this program, the elements of the arrays A() and B() are assigned the following values:

Array Value Array Value Array Value
A(0,0) 5 A(0,1) 1 A(0,2) 1
A(1,0) 1 A(1,1) 6 A(1,2) 1
A(2,0) 1 A(2,1) 1 A(2,2) 7
B(0,0) 15 B(0,1) 3 B(0,2) 3
B(1,0) 3 B(1,1) 18 B(1,2) 3
B(2,0) 3 B(2,1) 3 B(2,2) 21

Note that in line 60 the brackets around n%*n% are necessary, as with a simple array assignment. The amount being added, subtracted, and so on may be either a constant, a variable, a function result or an expression, provided that it is enclosed in brackets. However, you can use shorthand versions for addition and subtraction which do not require brackets:

60 A() += n%*n%
70 A() -= m%

It is also possible to add, subtract, multiply or divide two arrays, provided that they are of the same size. In the result, every element is obtained by performing the specified operation on the two elements in the corresponding positions in the operands.

For example, for two arrays which have been DIMmed A(1,1) and B(1,1), the instruction

A() = A() + B()

is equivalent to the following four instructions:

A(0,0) = A(0,0) + B(0,0)
A(0,1) = A(0,1) + B(0,1)
A(1,0) = A(1,0) + B(1,0)
A(1,1) = A(1,1) + B(1,1)

BASIC will perform proper matrix multiplication on pairs of two-dimensional arrays using the . operator. The first index of the array is interpreted as the row and the second as the column. For example:

10 i=2:j=3:k=4
20 DIM A(i,j),B(j,k),C(i,k)
30 :
40 REM Set up the array contents...
50 :
60 C() = A().B()

Note that the second dimension of the first array must be identical to the first dimension of the second array.

Also, the matrix multiplication operation can multiply a vector (a one-dimensional array) by a two dimensional matrix to yield a vector. There are two possible cases:

row().matrix()

This gives a row vector as the result. The number of elements is equal to the number of columns in the matrix.

matrix().column()

This gives a column vector as the result. The number of elements is equal to the number of rows in the matrix. For example:

 10 i = 2: j = 3
 20 DIM row(i), column(j)
 30 DIM matrix(i,j)
 40:
 50 REM lines to set up the arrays
200 column() = matrix().column()
220 PROCprint(column())
260 row() = row().matrix()
270 PROCprint(row())

Array operations

Arithmetic operations on arrays are not quite as general as those on simple numbers. Although you can say a=b*b+c, you cannot use the equivalent array expression a()=b()*b()+c(). Instead, you would have to split it into two assignments:

a() = b()*b()
a() = a()+c()

Also, the only place these array operations may appear is on the righthand side of an assignment to another array. You cannot say

PRINT a()*2

for example (or, indeed, PRINT a()).

The table below gives a complete list of array operations.

array = array Copy all elements
array = -array Copy all elements, negating
array = array + array Add corresponding elements
array = array - array Subtract corresponding elements
array = array * array Multiply corresponding elements
array = array / array Divide corresponding elements
array = factor Set all elements
array = factor, expression, ... Set several elements
array = array + factor Increment (or concatenate) all elements
array = factor + array
array += expression
array = array - factor Decrement all elements
array = factor - array
array -= expression
array = array * factor Multiply all elements
array = factor * array
array = array / factor Divide all elements
array = factor / array
array = array . array Matrix multiplication

array - means any array variable. All of the operations on two arrays require arrays of exactly the same size and type (real and integer arrays are treated as different types for this purpose). Only the assignment and concatenation operations are available on string arrays.

factor - means a simple expression, such as 1, LENA$ or "HELLO". If you want to use an expression using binary operators, it must be enclosed in brackets: (a+b).

The arrays used in these operations may all be the same, or all be different, or somewhere in between. For example, you are allowed to use:

a() = b() + c()
a() = a() + b()
a() = a() + a()

The matrix multiplication operator works on two arrays which must be compatible in size. This means that in the assignment

a() = b().c()

the following DIMs must have been used:

DIM b(i,j) : REM left side  is i rows by j columns
DIM c(j,k) : REM right side is j rows by k columns
DIM a(i,k) : REM result     is i rows by k columns

In addition, the following would be permitted:

DIM b(i,j) : REM left side is i by j matrix
DIM c(i)   : REM right side is column vector
DIM a(i)   : REM result is column vector

or

DIM b(k)   : REM left side is row vector
DIM c(j,k) : REM right side is j by k matrix
DIM a(j)   : REM result is row vector

There are some functions which act on single arrays:

  • SUM array gives the sum of all elements of the array or the concatenation of all the strings (up to 255 characters)
  • SUMLEN array gives the sum of the lengths of all of the strings in an array
  • MOD array gives the modulus, or square root of the sum of the squares of the elements of a numeric array. For example, if you had the following statements:
     10 DIM a(100)
     20 ...
     90 mod=MODa()
    then to perform the same operation without the MOD operator, you would have to say:
     10 DIM a(100), b(100)
     20 ...
     90 b()=a()*a()
    100 mod=SQR(SUM(b())

This edition Copyright © 3QD Developments Ltd 2015
Last Edit: Tue,03 Nov 2015