Array Overview

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 one-dimensional 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

2

3

4

87

93

77

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.  

 

Here is an example that finds the average of the four values in the scores array.

 

 

Note that it is as easy for the programmer to process 4,000 values as it is to process  four  by simply changing the value of the variable howmany!  That is part of the power of arrays used with counting loops.