Page 1 of 1

Smart BASIC Programming. Lesson 5

Posted: Fri Nov 14, 2014 5:31 am
by Mr. Kibernetik
Lesson 5 - Arrays

Besides usual variables, in smart BASIC you can also use arrays. Array is a variable which can hold multiple values. Each value is addressed by using index. For example, command

Code: Select all

DIM M (100)
creates array M, which contains 100 values. You can access array values by indicating value index in brackets. Indices start with 0, so this array M contains values with indices from 0 to 99.

Code: Select all

N = 12 'number of array values
DIM X (N) 'create array
'fill array
FOR K = 0 TO N - 1
  X (K) = K ^ 2
NEXT K
'read array
FOR K = 0 TO N - 1
  PRINT K; X (K)
NEXT K
Arrays can be one-, two- or three-dimensional:

Code: Select all

DIM X (30) 'one-dimensional array
DIM Y (20,30) 'two-dimensional array
DIM Z (50,50,50) 'three-dimensional array
Using command OPTION BASE 1 it is possible to set that indices start with 1 instead of 0:

Code: Select all

OPTION BASE 1
DIM X (12) 'create array
'fill array
FOR K = 1 TO 12
  X (K) = K ^ 2
NEXT K
'read array
FOR K = 1 TO 12
  PRINT K; X (K)
NEXT K
EXERCISE 6
Draw maze on the screen.