Suppose you wanted to read in 5000 integers and do something with them. How would you store the integers?
You could use 5000 variables, lapsing into:
     aa, ab, ac, ad, ... aaa, aab, ... aba, ...
or you could use an array.
An array contains several storage spaces, all the same type. You refer to each storage space with the array name and with a subscript.
The type definition is:
     type
        typename
= array [enumerated_type] of
another_data_type;
The data type can be anything, even another array. Any enumerated
type will do. You can specify the enumerated type inside the
brackets, or use a predefined enumerated type. In other words,
   type
      enum_type = 1..50;
      arraytype = array [enum_type] of integer;
is equivalent to
   type
      arraytype = array [1..50] of integer;
This is how strings are actually managed. You declare:
type String = packed array [0..255] of char;and use some kind of terminating character to signify the end of the string. Most of the time it's the null-character (ordinal number 0, or ord(0)). The packed specifier means that the array will be squeezed to take up the smallest amount of memory. This makes it possible to print out the entire array all at once rather than one-character at a time. In Turbo/Borland Pascal, there's a built-in string data type.
Arrays are useful if you want to store large quantities of data
for later use in the program. They work especially well with for
loops, because the index can be used as the subscript. To read
in 50 numbers, assuming
     type arraytype = array[1..50] of integer;
and
     var myarray : arraytype;
you use
for count := 1 to 50 do read (myarray[count]);
Brackets [] enclose the subscript when referring to arrays.
     myarray[5] := 6;
![]() Previous lesson |
![]() Next lesson |
![]() Contents |
![]() Index |
![]() e-mail me |