An array is a collection of fixed size of data items of the same type. The benefit is that you can store a whole set of values under 1 single name only. You can refer to each one separately, as each value is stored in a separate cell.
The general definition is |
var |
arrayname:array [indextype] of type; |
As type declaration:
type |
arrint=array[1..100] of integer; |
var |
arrint1:arrint; |
var |
arrint:array[1..100] of integer; |
1. To print an array from the reverse onto the screen.
|
2. To read values into an array
|
Rules:
The word 'array' must be preceded by the word 'packed'.
The lower index value must be 1.
type |
charstring = packed array [1..5] of char; |
var |
name1,name2,name3 : charstring; |
begin |
name1:='Susan'; |
name2:='Mary '; {Note that you could not have written 'Mary' as the size must be 5.} |
name3:=name1; |
writeln(name1); |
if name1>name2 then ..... {you can compare them according to the alphabetical ordering} |
end. |
Disadvantage:
Furthermore, you have to supply spaces whenever your input is smaller than the specified size;
Arrays of arrays are called multidimensional arrays. There could arise any reason for using this.
General form of declaration: arraytype = array [index1, index2, ... , indexn] of elementtype;
This is the same as array [index1] of array [index2] of .. array[indexn] of elementtype;
Example:
A : array [ 1..5,10..12,6..9,0..3] of integer;
Then they are referred to as: A[1,10,3,0] and can be used as simple integers.