Arrays

Arrays

Top  Previous  Next

It is often useful to handle variables as a group when the variables have something in common--like points on a graph or dollars in accounts. In XPL0 variables can be grouped using a single name with each item having a separate number. Such a group is called an array. For example:

 

        Account(11)

 

This refers to the 12th item in the array named "Account". If there are 20 items in an array, they are numbered 0 through 19.

 

In XPL0 there are three types of arrays: integer, real, and character.

 

Integer arrays are groups of variables where each variable is an integer. Each variable in the array can store a 2-byte value in the range -32768 through 32767 (or $0000 through $FFFF).

 

The name of an array must be declared before it can be used. Integer array declarations have the general form:

 

        integer NAME(DIMENSIONS), ... NAME(DIMENSIONS);

 

For example:

 

        integer Account(20);

 

This sets aside memory space for 20 integers and gives this space the name "Account". Now, values can be moved in and out of the elements of this array. For example:

 

        begin

        Account(19):= 2050;

        I:= Account(9) + 100;

      . . .

Array variables are normally used with an item number in parentheses. This number is called a "subscript", and it can be any integer expression as long as it evaluates to an item number that is in the array.

 

        Account(I+2):= J;

        if Account(0)=$0C then FormFeed;

 

Arrays that contain real numbers are similar to integer arrays. Here is an example:

 

        real  Dollars(70), X;

        int   I;

        begin

        for I:= 0, 70-1 do Dollars(I):= 0.00;

        Dollars(7):= 1.25;

        X:= Dollars(7) - 1.00;

        end;

 

Note that subscripts are always integers, or integer expressions, even for a real array.

 

Array elements can also be single bytes. Since a byte is often used to store an ASCII character, these arrays are called character arrays. Here are some examples:

 

        character  Name(20), Address(20), City(10), State(2);

 

Character arrays can have subscripts larger than 32767 (or $7FFF). It is logical to use hex numbers in this case (although negative decimal numbers can be used).

 

Here is detailed information about arrays:

 

Example Program: DICE
How arrays work *
Strings *
Multidimensional Arrays *
Complex Data Structures *
Constant Arrays *
Example Program: RECORDS *
Address Operator *
Returning Multiple Values *
Segment Arrays *