ARRAYS
An array is a structure which holds many variables, all of the
same data type. The array consists of so many elements, each element
of the array capable of storing one piece of data (ie, a variable).
An array is defined as follows,
type array_name = ARRAY [lower..upper] of data_type;Lower and Upper define the boundaries for the array. Data_type is the type of variable which the array will store, eg, type int, char etc. A typical declaration follows,
type intarray = ARRAY [1..20] of integer;This creates a definition for an array of integers called intarray, which has 20 separate locations numbered from 1 to 20. Each of these positions (called an element), holds a single integer. The next step is to create a working variable to be of the same type, eg,
var numbers : intarray;Each element of the numbers array is individually accessed and updated as desired.
To assign a value to an element of an array, use
numbers[2] := 10;This assigns the integer value 10 to element 2 of the numbers array. The value or element number (actually its called an index) is placed inside the square brackets.
To assign the value stored in an element of an array to a variable, use
number1 := numbers[2];This takes the integer stored in element 2 of the array numbers, and makes the integer number1 equal to it.
Consider the following array declarations
const size = 10; last = 100; type sub = 'a'..'z'; color = (green, yellow, red, orange, blue ); var chararray : ARRAY [1..size] of char; {an array of 10 characters. First element is chararray[1], last element is chararry[10] } intarray : ARRAY [sub] of integer; {an array of 26 integers. First element is intarray['a'] last element is intarray['z'] } realarray : ARRAY [5..last] of real; {an array of 95 real numbers. First element is realarray[5] last element is realarray[100] } artstick : ARRAY [-3..2] of color; {an array of 6 colors. First element is artstick[-3] last element is artstick[2] } huearray : ARRAY [color] of char; {an array of 6 characters. First element is huearray[green] last element is huearray[blue] }