• Keine Ergebnisse gefunden

ARRAY VARIABLES

Im Dokument BASIC Language Reference vs (Seite 52-59)

CHAPTER 3 DATA FORMATS

3.5 ARRAY VARIABLES

An array variable is a collection of scalar variables identified by a common name. Each scalar variable contained in the array is called an element of the array, and can be identified by specifying the array name followed by a subscript or pair of subscripts, which locate the element within the array. Arrays, like scalar variables, can hold floating point, integer, or alphanwneric data. A single array cannot hold values of more than one type. The names of array variables are formed in the same way as the names of scalar variables (a sequence of 1 to 64 letters, digits, and underscores, as described in

Section 3.3.3. Names of integer arrays must end in the percent sign (%) and those of alpha arrays must end in the dollar sign ($)). The one additional restriction on array names is that they cannot begin with the characters FN.

NOTE

Any attempt to use a name beginning with FN for an array will result either in an error message at compilation time or in a logically incorrect object program. When MINANS is set to YES, any name beginning with FN and containing parentheses is interpreted by the compiler to refer to a user-defined function (refer to Section 4.4.2).

In general, any reference to an array variable must consist of the array name followed by parentheses. If the parentheses enclose an expression or a pair of expressions, the expressions are interpreted as the subscripts of a particular element in the array. For example, the fifth element in floating-point array N() can be specified as N(5); BOX$(K) refers to the K-th element of the alpha array BOX$().

Note that the subscript is enclosed in parentheses immediately following the array name. In situations in which the entire array

(rather than a particular element of the array) is to be referenced,

you must follow the array name by empty parentheses, e.g., N() or BOX$(), to form an "array-designator." The array name alone (e.g., N or BOX$) is used only in ~pecial matrix statements (e.g., MAT INPUT and MAT PRINT) .

Since scalar variables are different from array variables, the same name (i.e., the same sequence of letters, digits, and underscores) can be used both as a scalar variable name and as an array variable name.

Thus N() designates an array variable, while N names a scalar variable, except in a matrix statement. In any statement except a matrix statement (refer to Section 9.2), the array must always be referenced with an array-designator to indicate an array rather than a scalar variable. For example,

WHALE Identifies a floating-point scalar variable.

WHALE% Identifies an integer scalar variable.

WHALE() Identifies a floating-point array.

WHALE%() Identifies an integer array.

WHALE$ Identifies an alphanumeric scalar variable.

WHALE$() Identifies an alphanumeric array.

To minimize the chance of confusion, however, using the same name for scalar and array variables in a program is not recommended.

In addition, when the compiler option MINANS = YES is selected, arrays ~ and scalars of the same name are not permitted in the same program.

The array base can optionally start with zero or one, allowing ranges of 0-N rather than 1-N. Also, an array need not be explicitly

dimensioned, as the number of dimensions is taken from its first use.

All floating subscripts are rounded rather than truncated. The default base is zero.

3.5.1 One-Dimensional and Two-Dimensional Arrays

Array variables can be either one-dimensional or two-dimensional. A one-dimensional array is a list of all variables identified by the same name. A two-dimensional array is a table of variables all identified by the same name.

A one-dimensional array can be thought of as a list or column of variables (elements), each occupying its own slot, or row, in the column. Consider, for example, the representation of array DWARF() in Figure 3-1.

DWARF() Row 1 DWARF(l) Row 2 DWARF(2) Row 3 DWARF(3) Row 4 DWARF(4) Row 5 DWARF(5)

Figure 3-1. The One-Dimensional Array DWARF()

Note that DWARF() contains a total of five elements and that each element is identified by specifying its row. For example, element DWARF(3) is located in row 3.

One-dimensional arrays are also called lists, vectors, colwnn vectors, and, since each element is identified by a single subscript,

singly-subscripted arrays.

The scheme in Figure 3-1 can be generalized to contain two or more coltUOns. When this is done, the result is a two-dimensional array. A two-dimensional array can be thought of as a table consisting of two or more columns of elements. Consider, for example, the

representation of the two-dimensional array HOBBIT() in Figure 3-2.

~

\

HOBBIT()

Column 1 Column 2 Column 3 Row 1 HOBBIT(l,l) HOBBIT ( 1, 2) HOBBIT(l,3) Row 2 HOBBIT(2,l) HOBBIT(2,2) HOBBIT(2,3) Row 3 HOBBIT(3,l) HOBBIT(3,2) HOBBIT(3,3) Row 4 HOBBIT(4,l) HOBBIT(4,2) HOBBIT(4,3) Row 5 HOBBIT(5,l) HOBBIT{5,2) HOBBIT{S,3)

Figure 3-2. The Two-Dimensional Array HOBBIT()

Note that HOBBIT() consists of three columns of elements, with five rows in each column, for a total of 15 elements. In this case, it is not sufficient to identify each element by its row, since the element may be in column l, column 2, or column 3. A second subscript is required to identify the column. The convention followed when

referencing a particular element in a two-dimensional array is always to specify the row first, and then the column. Thus HOBBIT(3,2) identifies the element in row 3 and column 2.

Two-dimensional arrays are also called tables or matrices, and, because each element is identified by a pair of subscripts, doubly-subscripted arrays.

Elements in an array can be ref erred to by subscripts that are legal BASIC expressions. Thus JIM(N) refers to the N-th element of array JIM() for whatever value N has at the time of execution. This ability to reference an array by a variable subscript is one of the useful features of arrays, since it can eliminate a considerable amount of repetitive coding.

For example, the following three statements 100 FOR I = 1 TO 50

200 PRINT JIM(I) 300 NEXT I

cause the first 50 elements of array JIM() to be printed with considerably less coding than 50 consecutive PRINT statements.

NOTE

If the value of an expression used as a subscript is not an integer at run time, the value of the expression is truncated and the integer value is used as the subscript when the compiler option MINANS = NO.

When the compiler option MINANS=YES is selected, the value of the expression is rounded.

3.5.2 Dimensioning an Array

When a program is compiled, the BASIC compiler reserves storage space for each variable. To do this, the compiler must know how much space to allocate for each variable. Since arrays can be either one- or two-dimensional and can contain varying amounts of data, you must tell the compiler how much space to reserve for each array in a program;

that is, the array must be dimensioned. An array is dimensioned by specifying whether it has one or two dimensions and how many rows (and columns, if two-dimensional) are in the array. Dimension information is specified using either the DIM {dimension) or COM (common)

statement. For example, to allocate space for a one-dimensional integer array of 10 elements named VEGETABLE%{), write

DIM VEGETABLE%(10)

If VEGETABLE%{) is to be used by more than one program or subprogram running together, use COM instead of DIM.

You can use DIM and COM statements to define any number of arrays of any type, as long as each array is separated from the one following it by a comma. When using DIM or COM to dimension an alpha array, the length of each element in the array can be specified as an integer immediately following the right parenthesis.

For example,

DIM NAME$(500)10, CITY{lOO), STATE(S,10) COM CODE$(20,10)5, ZIP%(1000), COUNT%

defines a 500-element, one-dimensional, alphanumeric array (NAME$()) where each element is 10 bytes long; a 100-element one-dimensional floating-point array (CITY()); a 5-row by 10-column two-dimensional floating point array (STATE()); a 20-row by 10-column two-dimensional alpha array (CODE$()) with each element 5 bytes long; and a

1000-element one-dimensional integer array (ZIP%). The last two arrays are designated as common, as is the integer scalar COUNT%.

Using DIM and COM statements to specify the length of alpha scalars is discussed in Section 3.4.2.

~- I

If an array is not dimensioned before its first occurrence in an executable program statement, the compiler automatically assigns default dimensions of 10 rows by 10 columns, unless the compiler option MINANS

=

YES is selected. In this case, the number of

dimensions is taken from the first use of the array. In the case of alpha arrays, each element is assigned a default length of 16 bytes when the compiler option MINANS

=

NO and 18 bytes when MINANS

=

YES.

Therefore, any array that is to be of any other dimension must be dimensioned before its first occurrence in an executable statement.

No array can be dimensioned more than once in a program. Row and column dimensions specified in DIM or COM statements must be between 1 and 32,767.

The total size of all the variables in a program, including array variables, cannot exceed the available segment 2 address space. The segment 2 address space is limited to a maximum of 512K (524,288) bytes on all VS systems. If the variables in a source program require more than 512K bytes of storage, the compiler issues an error message and halts code generation.

Although programs can be compiled with up to 512K bytes of variable storage space (segment 2 space), the resulting object program cannot be executed unless there is sufficient space available on the

particular VS configuration at run time. In addition, each user ID has an associated maximum segment 2 size that can be less than or equal to the system maximum; a program will not run unless a user's segment 2 size is sufficiently large.

Thus, the fact that a particular program compiled successfully on a particular system does not guarantee that it will also run on that system. For example, on a VS system with only 256K bytes of Segment 2 space allocated to each task, a program requiring 400K of Segment 2 space will compile with no errors (assuming it were syntactically correct), but will not run because of insufficient memory to store the variables during execution. Likewise, a user with a segment 2 size of 256K bytes on a system with a maximum of 512K bytes can compile a program requiring 400K bytes of variable storage, but the compiled program will not run.

Since DIM statements are processed during compilation, prior to program execution, they cannot be supplied with variable subscripts, since the value of the variable is unknown at that time. The

following statement, for example, produces an error message:

DIM A1(5,N)

n

'

. ~:

CHAPTER 4

NUMERIC OPERATIONS

4.1 INTRODUCTION

Numeric data (refer to Section 3.3) is manipulated in VS BASIC by means of operators and functions. An operator is a symbol (such as + or -) that specifies some operation (such as addition or subtraction) to be performed, usually involving two numeric quantities.

Section 4.2 discusses operators.

A function is a construct that performs some series of operations.

These operations may or may not be performed on one or more input values (called arguments) to return a single output value. For example, SIN(X) and SQR(X) are functions that calculate the sine and square root of an argument, in this case of the variable X.

Section 4.4 discusses functions.

A number of numeric constants, variables, and functions connected by numeric operators constitutes a numeric expression. When values are supplied for any variables in an expression, the value of the

expression is determined by performing the indicated operations and functions. This occurs when a statement containing an expression is executed when a program is run. The value of the expression is then used in whatever way is indicated by the particular statement being processed. Section 4.3 discusses expressions.

Im Dokument BASIC Language Reference vs (Seite 52-59)