typestar

Static arrays in Delphi

Five rainfall figures in an array[0..4] of Integer, walked once for a total and a peak.

program StaticArrays;

// A static array's index range is fixed when the program is compiled.
const
  Rainfall: array[0..4] of Integer = (4, 12, 0, 9, 21);
var
  I, Total, Wettest: Integer;
begin
  Total := 0;
  Wettest := Rainfall[Low(Rainfall)];
  // Low and High report the bounds, so the loop follows the type.
  for I := Low(Rainfall) to High(Rainfall) do
  begin
    Total := Total + Rainfall[I];
    if Rainfall[I] > Wettest then
      Wettest := Rainfall[I];
  end;
  WriteLn('total ', Total, ' mm, wettest ', Wettest, ' mm');
end.

How it works

  1. Rainfall: array[0..4] of Integer = (4, 12, 0, 9, 21); fixes the bounds at compile time.
  2. for I := Low(Rainfall) to High(Rainfall) do takes its range from the array itself.
  3. The body adds each value into Total and keeps the largest in Wettest.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
507
Tokens
119
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 95 seconds.

Type this snippet

Step 1 of 3 in Arrays & records, step 13 of 27 in Language basics.

← Previous Next →