An array type declares that a variable or constant can hold multiple values. So arrays are like lists. Generally the elements (individual values) of an array are typed (i.e., an array that holds integers must be declared as such), but there are some cases in which an array can be untyped. Also, in most cases the bounds, or start and stop index, of an array must also be specified. The bounds of an array can be any valid subrange. The following is the most basic example of declaring an array:
type
  T5IntArray = array[1..5] of Integer;
Once a type is declared, we can declare instances of this type as shown below:
var
  IntArray1,IntArray2 : T5IntArray;
We can assign a value to these array variables using the following syntax within a begin-end block:
var
  Int : Integer;
begin
  IntArray1[1] := 10;
  IntArray1[2] := 20;
  IntArray1[3] := 20;
  Int := IntArray1[1]; // Int now holds the value 10
  IntArray2 := IntArray1; // Arrays themselves can be assigned if their
                          //   types match
end.
Of course, we already have seen basic examples like this through the use of the Keyboard array defined in FTGraphics. The index range of an array can be anything the programmer chooses. One of the most common indexing schemes is the 0-indexed array. Because the array starts with 0, the last index is the number of elements minus 1.
type
  // Declaration of a basic 0-indexed array type that can contain 5 reals
  T5RealArray = array[0..4] of Real;
When character types are used in arrays, the array is essentially a string of fixed length:
type
  // Declaration of a 0-indexed array of characters
  T10CharArray = array[0..9] of Char;
var
  Str : T10CharArray;
begin
  // Character arrays can be assigned like strings, but the length of the
  //   string must be less than or equal to the length of the array
  Str := '0123456789';
end.
If an array needs to be resized at runtime (i.e. not have a fixed length), dynamic arrays can be used. The key here is that the length must be set inside the begin-end block before the array is used. The starting index of a dynamic array is always 0.
type
  // Declaration of a dynamic array that can contain integers
  TDynIntArray = array of Integer;
var
  DynIntArray : TDynIntArray;
begin
  // Dynamic arrays: Always 0-indexed. Must use SetLength before
  //   assigning values.
  SetLength(DynIntArray,2);
  DynIntArray[0] := 10;
  DynIntArray[1] := 20;
  SetLength(DynIntArray,3); // Dynamic arrays can be resized without corrupting
                            //   existing data
  Writeln(DynIntArray[1]);  // Output: 20
end.
Occasionally we will have a need for a multidimensional array. Object Pascal provides two equivalent ways of declaring multidimensional arrays. Indexing can be achieved with syntax similar to Matrix[1,2] or Matrix[1][2]. Both are equivalent.
// Multidimensional array
  T4X4Matrix = array[1..4,1..4] of Integer;

  // Multidimensional array: Equivalent to the one above
  TAnother4X4Matrix = array[1..4] of array[1..4] of Integer;

var
  Matrix : T4X4Matrix;
  AnotherMatrix : TAnother4X4Matrix;
  Int : Integer;
  Num : Real;
  ArrayVar : array[1..10] of String;
    // Note: Arrays can be created without creating an array type first
begin
  // Multidimensional array assignment
  Matrix[1,2] := 3;
  Matrix[1][2] := 3; // Either syntax is valid and identical
end.
Enumerated values can also be used as bounds for arrays since they are ordinal types. Enum-indexed arrays demonstrate that any ordinal type (even characters like 'A'..'Z') can be used to index an array.
type
  // Array indexed by an enumeration
  TNumber = (numZero,numOne,numTwo,numThree,numFour,numFive,
    numSix,numSeven,numEight,numNine);
  TEnumIndexedArray1 = array[TNumber] of Integer;
  TEnumIndexedArray2 = array[numTwo..numFive] of Integer;

var
  EnumIndexedArray1 : TEnumIndexedArray1;
  EnumIndexedArray2 : TEnumIndexedArray2;
begin
  // Enum-indexed arrays
  EnumIndexedArray1[numOne] := 1;
  EnumIndexedArray2[numFour] := 4;
end.
Instances of arrays can be created without declaring the type first, but this is generally not the most appropriate way of creating arrays because the type cannot be efficiently reused.
var
  SomeArray : array[1..5] of Integer;
begin
  SomeArray[1] := 10;
end.
Finally, one last very important note with structured types such as arrays is how to pass them to procedures and functions. Remember that the default behavior of subroutine calls is to pass arguments by value unless the "var" keyword is used in which case the arguments are passed by reference. Arrays should always be passed by reference. The reason is that passing by value means making a copy of the entire data structure (which can get fairly large for arrays). For example, run the following program to see the size of the declared array:
{$APPTYPE CONSOLE}
program Workspace;

type
  TSomeArray = array[0..99] of Integer;

var
  SomeArray : TSomeArray;
begin
  Writeln(SizeOf(SomeArray));
  Readln;
end.
An Integer by itself takes up 4 bytes. When it is placed into an array with 100 elements, the array takes up 400 bytes. If we were to pass SomeArray by value to a subroutine, the subroutine would have to make a copy of 400 bytes each time it executes. This is equivalent to calling a subroutine with 100 Integer arguments. On the other hand, if SomeArray is prefixed with the var keyword, nothing is copied. Of course, passing an argument with the var keyword means that any modifications to it inside the procedure are reflected when the call returns. If this is not the desired behavior, Object Pascal allows the use of the const keyword instead of var which keeps the compiler from allowing any assignments to the variable. Most of this will make more sense after we complete an exercise with arrays.
Excercise 3-9.
Write code to solve the following problems.
  1. Declare an array from 0 to 100 of real numbers and make a variable instance of this array.
  2. Write a program that reads in a series of numbers until the user enters a blank. Since we are dealing with blanks, you will need to read the number in using Readln as a string and then convert it to a number using StrToFloat from SysUtils. When you convert the number store it in RealArray[0]. This will of course overwrite the first entry in the array on each iteration of the loop, but we will worry about this in the next problem.
  3. Now modify the program from the previous problem so that values read in are placed in sequentially increasing slots in the array. Hint: Use a counter variable.
  4. Now write a function called Mean that takes a TRealArray argument and an Integer that specifies the number of values in the array (i.e. the count) and computes the mean of the array. Keep in mind that TRealArray is huge (it contains 101 reals). That means we should pass it by reference using the const directive. (We would use var if we wanted to modify the array, but in this case we don't.) Output the mean to the screen.
  5. Add a function that outputs the sample standard deviation. The formula for the sample standard deviation is given by the following:
      In this formula, is the number of entries in the array. is each individual entry (i.e. RealArray[K]) and is the mean. For comparison, the formula for mean is the following:
      Test your code with the list 1, 2, 3, 4, 5. The mean is 3 and the sample standard deviation is approximately 1.581.