Array variables

Description

Array variables are used to store values of array types (i.e. collections of values of the same type). The type of each value in an array is called the array's component type. An array's component type may be any type, including an array type, so it is possible to define arrays of arrays (also called multi-dimensional arrays).

A reference to an array variable is a reference to the entire array as a unit, and can be used to both store array values into, and retreive array values from the array variable.

The number of values in each array is fixed, and each value in the array is identified by a value of the array's index type (which is specified when the array is defined). Each value in an array is stored in a seperate component of the array called an array element (or an indexed variable). Each element of an array can be referenced individually, in order to store values into, and retrieve values from specific elements. See the  syntax  section below for details.

Example

Below are some examples of array variable declarations.

   scores : array[1..200] of integer;
   name : array[IndexType] of real;
   students : array[1..MAX_STUDENTS] of student;
   grades : array[1..10] of array[1..5] of integer;
   grades : array[1..10, 1..5] of integer;

NOTE: These examples assume that IndexType is a previously defined ordinal type, MAX_STUDENTS is a previously defined constant of integral type, and student is a previously defined type. Also the two declarations shown for the array grades are equivalent.

Here is a simple example program that generates some random numbers, stores them in an array, sorts the numbers, and then prints the numbers out.

program sort(output);
const
   max = 20;
var
   numbers : array[1..max] of integer;

   procedure GenerateRandomNumbers;
   var
      i : 1..max;
   begin
      for i := 1 to max do
         numbers[i] := random(100)+1;
   end;

   procedure SortNumbers;
   var
      i : 1..max;
      swapped : boolean;
      temp : integer;
   begin
      repeat
         swapped := false;
         for i := 1 to max-1 do
            if numbers[i] > numbers[i+1] then
               begin
                  temp := numbers[i];
                  numbers[i] := numbers[i+1];
                  numbers[i+1] := temp;
                  swapped := true;
               end;
      until not swapped;
   end;

   procedure PrintNumbers;
   var
      i : 1..max;
   begin
      for i := 1 to max do
         writeln(i, numbers[i]);
   end;

begin
   randomize;
   GenerateRandomNumbers;
   SortNumbers;
   PrintNumbers;
end.

Syntax

See array types for the syntax for defining new array types.

The syntax for referencing array elements (i.e. indexed variables) is given below:

(NOTE: for clarity some parts of the syntax are omitted, see Irie Pascal Grammar for the full syntax):

   indexed-variable = indexed-variable-array | indexed-variable-string

   array-variable = variable-access

   index-expression = expression

   indexed-variable-array = array-variable '[' index-expression { ',' index-expression } ']'

Where array-variable is a reference to an array variable, and index-expression is an expression which evaluates to one of the values specified by the array's index type.