QMBasic - Scalars, matrices and dynamic arrays

QMBasic  -  Scalars, Matrices and Dynamic Arrays

Top  Previous  Next


QMBasic provides support for both scalar and matrix variables. A scalar variable is a simple value referenced by its name alone. It may contain data of any type.


A matrix variable is a one or two dimensional array of values. Matrices must be declared by use of the DIMENSION (more usually DIM) statement. Because memory for matrices is allocated dynamically, the DIM statement must be executed at program run time before the variable is used in any other way. Matrix variables are also known as dimensioned arrays.


A one dimensional matrix of ten elements is defined by a statement of the form

DIM A(10)


For a two dimensional matrix with 5 rows of 8 columns this becomes

DIM B(5,8)


A single dimensional matrix is effectively a two dimensional matrix with one column. Thus references of the forms A(B) and A(B,1) are totally interchangeable.


By default, all matrices have an additional element, the zero element, which is used by some QMBasic statements. This is referred to as A(0) or B(0,0). The $MODE compiler directive can be used to create Pick style matrices which do not have a zero element. Note that, in a two dimensional matrix, this is a single element, not a complete row 0 and column 0.


The elements of a matrix may be of differing types (numbers, strings, file variables, etc).



A variable holding a string value may be considered as a dynamic array, the mark characters being used to divide it into fields, values and subvalues. Such a string may correspond to a record in a data file or may be totally internal to the program. Special operations are provided in QMBasic to manipulate dynamic arrays. These include sorted and unsequenced searching, insertion, deletion, replacement and extraction as well as some extremely powerful operations to build or decompose dynamic arrays.


A dynamic array in which each field, value or subvalue contains a numeric value is known as a numeric array. Many of the arithmetic operations operate on numeric arrays by processing corresponding elements in turn. For example, a statement


A = B + C


adds B and C together, storing the result in A. Where B and C are simple numeric values or strings that can be converted to numbers, this operation behaves as in most other computer languages. If B and C are dynamic arrays the operation handles each corresponding pair of values in turn.


B = "1" : @FM : "2" : @VM : "3" : @FM : "4"

C = "5" : @FM : "6" : @VM : "7" : @FM : "8"

A = B + C


The result of this operation would be to set A to  6FM8VM10FM12. The effect of operations on numeric arrays where the placement of fields, values and subvalues do not match exactly is determined by the use of the REUSE() function.



See also:

Matrix File I/O