Array Constants

The following program shows how to declare and use an array constant. Array MonthNames is declared in the const section, and is used to store the names of the months of the year.

program  MonthlyRainfall2;
{$APPTYPE CONSOLE}
const
  MonthNames : array[1 .. 12] of string = ('January', 'February', 'March', 'April', 'May', 'June', 'July',
                                           'August', 'September', 'October', 'November', 'December');
var
  Rainfall : array [1 .. 12] of integer;
  Count, Total : integer;
begin
  for Count := 1 to 12 do
    begin
      write('Please enter rainfall for ', MonthNames[Count], ' ');
      readln(Rainfall[Count]);
    end; 
  Total := 0;
  writeln; 
  writeln ('Rainfall stored:');
  for Count := 1 to 12 do
    begin
      writeln(MonthNames[Count], ': ', Rainfall[Count], ' ');
      Total := Total + Rainfall[Count];
    end;
  writeln;
  writeln('Total for year: ', Total);
  readln;
end.
Features introduced:
  • Constant array declaration
  • Initialising constant arrays

The arrays so far have been one-dimensional arrays, also known as lists. The next part discusses two-dimensional arrays.

Programming - a skill for life!

How to declare and use arrays