DIMENSION, DIM |
![]() ![]() ![]() |
The DIMENSION statement is used to set the dimensions of a matrix. The short form DIM may be used in place of DIMENSION.
Format
DIMENSION mat(rows {, cols})
where
A matrix variable is a one or two dimensional array of values. Matrices must be declared by use of the DIMENSION statement. The DIMENSION statement must be executed at program run time before the variable is used in any other way.
A one dimensional matrix of ten elements is defined by a statement of the form
DIMENSION A(10)
For a two dimensional matrix with 5 rows of 8 columns this becomes
DIMENSION B(5,8)
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 select Pick style matrices which do not have a zero element.
The elements of a matrix may be of differing data types.
A matrix may be redimensioned at any time by a further DIMENSION statement though the number of dimensions cannot be changed. Existing values of matrix elements will be retained in the redimensioned matrix by copying elements on a row by row basis. If the matrix is enlarged , the newly created elements will be unassigned. If it is smaller than before, any values in the excess elements are discarded.
The INMAT() function may be used to check on the success of a DIMENSION statement. A sequence such as DIMENSION A(N) IF INMAT() THEN ABORT "Insufficient memory" will cause the program to abort if there is insufficient memory to hold the matrix. The INMAT() function used in this way returns 0 if the DIMENSION statement was successful, 1 if it failed. With the memory sizes found on modern systems, this test is probably totally unnecessary.
The INMAT() function can also be used to find the current dimensions of a matrix.
Example
N = DCOUNT(REC, @FM) DIM A(N) MATPARSE A FROM REC, @FM
This program fragment creates an array with the correct number of elements to receive the result of a MATPARSE operation on dynamic array REC and then performs the MATPARSE. |