Sie sind auf Seite 1von 2

Arrays in RAPTOR

An array is a collection of variables sharing the same name but accessed individually using an index. The
index appears in square brackets ([ ]) after the array variable name. Below is a picture of a onedimensional array named scores containing 4 elements (numbered 1 through 4). The names of the array
elements are shown, not their values.
scores[1]

scores[2]

scores[3]

scores[4]

The number in the square brackets is the index. It distinguishes between the different variables falling
under the name scores. Important Note: The index must be a positive integer; it cannot be 0 or
negative, and it cannot have a fractional part.
Below is a slightly different visualization, this time showing sample values in the variable memory
locations and the indexes above them.

Scores
1
87

2
93

3
77

4
82

The power of arrays in programs is that many values in an array can be processed in a counting
loop. The body of the counting loop operates on a single element of the array, but the fact that the loop
repeats and the counter is incremented means that each element of the array is ultimately processed
during loop execution. In fact, rarely is an entire array used in a single Raptor symbol; a program will
rarely use the name of an array without indexing it.

Creating Arrays
Array variables must be created before they are used. This creation occurs when an array element is
given a value via an assignment or an input. The array is created up to the highest indexed element
given a value. This is often done in a counting loop from one to some upper limit, so the array initially
has one element, then two, then three, and so on as the loop is repeated and the index increases.
A run-time error will occur if the program attempts to access an array element whose index is higher
than the index of any elements previously assigned a value.
However, the array elements may be assigned values in any order, with some indexes left out. In this
case, the highest index used still defines the upper limit of the array's size, but the entries in the array
not assigned a value by the program will default to a value of 0.

For example, an assignment of the form:

values[7] <- 3
results in a values array that looks like this:
1
0

2
0

3
0

4
0

5
0

6
0

7
3

A second assignment

values[9] <- 6
expands the array and makes it look like this:
1
0

2
0

3
0

4
0

5
0

6
0

7
3

8
0

9
6

One convenient side effect of this behavior is that the programmer can initialize an entire array to 0 with
a single assignment statement. For example, to initialize an array of 100 elements to 0, the single
assignment

values[100] <- 0
can be used in place of a counting loop that contains an assignment and executes it 100 times.
Creating Two-Dimensional Arrays
When creating two-dimensional arrays, the size of the array in both dimensions is determined by the
highest index assigned a value in that dimension. Again, a single assignment like

numbers[3,4] <- 13
results in a two-dimensional numbers array that looks like this:

1
2
3

1
0
0
0

2
0
0
0

3
0
0
0

4
0
0
13

Das könnte Ihnen auch gefallen