Arrays

Arrays are sequences of numeric variables or of string variables (each is an element of the array), in which each element is identified by number. The first element is element number 1 (not element number 0, as in C language). Arrays are the easiest way to write code in which the variable affected varies depending on a counting number.

A BASIC program creates an array with the  statement discussed below. Certain BASIC statements that return multiple values can create arrays to hold the values. Others require that the array already exist.

When you create a string array, elements that are not yet assigned a value have the value of the empty string. When you create a numeric array, elements that are not yet assigned a value have the value of 0.

The name of an array can be the name of a numeric variable (such as ), in which every element is a numeric value; or a string variable (such as  ), in which every element is a string value.
 * Syntax

The simplest array is an array of one dimension (a vector or linear array). If the variable A is a linear array of ten elements, its elements are referenced as  through. The number inside the brackets is called the index and specifies the array element to be used. The index does not have to be a numeric constant, but can be any numeric expression. The expression  operates on a different element depending on the value of.

A two-dimensional array (a matrix) can be imagined as a table. All references to this array have two indexes. The first might be the row in the table and the second might be the column. So  might represent information in the table's third row, fifth column.

You can specify a segment of a one-dimensional array using this same syntax. The first index is the element at which to start, and the second index is the number of elements in the segment. So  means an array segment containing ,  ,  ,  , and. Omitting the first index (such as ) assumes 1, which starts at element 1 of the array. Omitting the second index (such as ) continues to the end of the array. Therefore, omitting both (such as  denotes the entire array.  The syntax   also denotes the entire array.
 * Segments

Wherever this chapter specifies a vector or presents a variable whose name ends with _vec, it can be either an entire one-dimensional array, or a segment using the two-index syntax shown above.

The segment syntax doesn't apply to all arrays, only those DIM creates with a single dimension, when the program refers to them with two indexes instead of one. For example, if you create a matrix, there is no built-in syntax with which to specify a sub-matrix of it, though you can create a second matrix and copy entries to build such a sub-matrix.

When an RFO-BASIC! program creates an array, BASIC obtains memory from the Android device, sufficient to store each element, plus a bookkeeping area in which BASIC remembers the size of the array. There is no limit on the number of dimensions a BASIC array can have, nor on the number of elements in each dimension. The only inherent limitation on the size of an array is the available memory of the Android device.
 * Implementation


 * See also
 * Lists are similar to one-dimensional arrays, but provide BASIC statements that let you do things not possible with arrays, such as insert and delete elements and have subsequent elements change position.
 * Arrays are always passed to user-defined functions by reference, which means that code inside the function can read and modify any element of the array.
 * The LIST.ADD.ARRAY statement builds a list from values contained in an array.
 * The GR.SET.PIXELS draws a figure onto the Graphics Screen using x and y coordinates contained in an array.

DIM
Create one or more arrays

DIM arrayspec{, arrayspec}... where each  is: [{,}...]
 * Syntax

The DIM statement creates one or more arrays as specified in the statement. Each numeric expression specifies how many elements the array will have in that dimension. The number of numeric expressions following a variable specifies how many dimensions the array will have.
 * Description

A single DIM statement can create many arrays. Each can be either a numeric array or a string array. When RFO-BASIC! creates an array, it obtains memory from the Android device and initializes each element of the array. Elements of numeric arrays are initialized to 0 and elements of string arrays are initialized to the empty string.

DIM A[15] DIM B$[2,6,8], C[3,1,7,3], D[8]
 * Examples

The first example creates a one-dimensional numeric array (vector) with fifteen elements. The second example creates a three-dimensional string array, a four-dimensional numeric array, and a one-dimensional numeric array.

ARRAY.LOAD
Create a one-dimensional array and specify its initial values

ARRAY.LOAD [],, ...
 * Syntax

The ARRAY.LOAD statement creates a new, one-dimensional array with the specified. Unlike DIM, the writer does not specify the number of elements of the new array with a numeric expression between the brackets. Instead, it follows the brackets with a list of expressions. Each expression is loaded into an element of the new array, starting with element number 1. The size of the new array is the number of expressions in the list.

If the named array already exists, it is overwritten.

The array may be numeric (such as ) or string (such as  ). Each expression in the following list must be the same type as the array.

The list of expressions may be continued onto the next line by ending the line with the  character. The  character may be used between parameters where a comma would normally appear. The  itself separates the parameters; the comma is optional. The  character may not be used within a parameter to split it between two lines.

ARRAY.LOAD Numbers[], 2, 4, 8, n^2, 32
 * Examples

ARRAY.LOAD Hours[], 3, 4,7,0, 99, 3, 66~       % comma not required before ~ 37, 66, 43, 83,~                               % comma is allowed before ~ 83, n*5, q/2 +j

ARRAY.LOAD Letters$[], "a", "b","c",d$,"e"

ARRAY.COPY
Copies values from a vector to a one-dimensional array, possibly adding new elements

ARRAY.COPY ) or a segment (such as  start  length ).
 * Syntax

If  does not exist, then ARRAY.COPY creates it. If ARRAY.COPY includes , then all the specified elements are copied to it, and it also receives extra elements. Their initial values will be 0 in the case of a numeric array, or the empty string in the case of a string array.
 * If  is positive, then the specified number of elements are created after the elements copied from, included with the RFO-BASIC! installation, contains working examples of the ARRAY.COPY statement.

ARRAY.FILL
Fill a vector with a specified single value

ARRAY.FILL , 
 * Syntax

The ARRAY.FILL statement sets each element of a vector to the value of . The specified array must already exist. The  can be an entire array (such as ) or a segment (such as  start  length ). Either the vector and the expression must both be numeric or they must both be string.
 * Description

Deleting arrays
UNDIM {, ...}
 * Syntax

ARRAY.DELETE {, ...}

The UNDIM and ARRAY.DELETE statement are equivalent statements. Both delete one or more specified arrays. Deleting an array releases back to the Android device the memory that the array used. The array name cannot be used except if it is explicitly or implicitly created again. At that time, it does not need to have the same dimensions as it had before.
 * Description

Testing arrays
Two RFO-BASIC! statements let a program examine arrays it has already created:

ARRAY.DIMS start  length ), then this is the length of the segment.  (If length is provided, this is the result, except when there are fewer elements remaining in the array.)

Vector operations
Three RFO-BASIC! statements easily perform common operations on a vector (a one-dimensional array or a segment of one). Since these operations do not require repeat interpretation, they are much more efficient than coding a BASIC loop to perform the computations. The statements in this section can be used on numeric or string arrays. The statements operate by changing the values of elements. If a statement specifies a segment, then elements outside that segment are never changed.

ARRAY.SORT 


 * The ARRAY.SORT statement puts the values in the segment in ascending order. If there are duplicate values in the segment, there will still be duplicate values after executing ARRAY.SORT.

ARRAY.REVERSE 


 * The ARRAY.REVERSE statement puts the values in the segment in reverse order to the order they occupied at the start.

ARRAY.SHUFFLE 


 * The ARRAY.SHUFFLE statement puts the values in the segment in random order.

ARRAY.SEARCH
Search a vector

ARRAY.SEARCH , , <pos_nvar>{, <start_nexp>}
 * Syntax

The ARRAY.SEARCH statement searches <array_vec> for the first occurrence of <value_exp> and returns in <pos_nvar> the number, relative to the start of the vector, of this first occurrence. If <value_exp> does not occur in the area searched, the statement sets <pos_nvar> to 0.
 * Description

If <array_vec> is a one-dimensional array, then <pos_nvar> is the position of the occurrence. If <array_vec> is a segment, then it is the position relative to the start of the segment.

If <start_nexp> is present, then the search does not start at the start of the vector, but that many elements into the vector.

ARRAY.SEARCH conducts an exact search, with no heuristics such as rounding or treating capital and lowercase letters as equivalent.

ARRAY.SEARCH A[15,6], 9999, i, 3
 * Example

The vector to search is the segment consisting of the six elements A[15] through A[20]. The search begins at element number 3 of this segment; that is, at A[17]. Therefore, ARRAY.SEARCH examines elements A[17] through A[20]. It can set i to 3, 4, 5, or 6; or 0 if none of the four equals the desired value 9999.

Vector arithmetic
When a one-dimensional numeric array, or segment of one, exists and contains a series of numbers, several RFO-BASIC! statements easily perform arithmetic on this series.

ARRAY.MAX <max_nvar>,<array_vec>


 * Sets <max_nvar> to the highest value within <array_vec>.

ARRAY.MIN <min_nvar>,<array_vec>


 * Sets <min_nvar> to the lowest value within <array_vec>.

ARRAY.STD_DEV <sd_nvar>,<array_vec>


 * Sets <sd_nvar> to the standard deviation of the elements of <array_vec>.

ARRAY.SUM <sum_nvar>,<array_vec>


 * Sets <sum_nvar> to the sum of the elements of <array_vec>.

ARRAY.VARIANCE <v_nvar>,<array_vec>


 * Sets <v_nvar> to the variance of the elements of <array_vec>.