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
Rainfall: array[0..4] of Integer = (4, 12, 0, 9, 21);fixes the bounds at compile time.for I := Low(Rainfall) to High(Rainfall) dotakes its range from the array itself.- The body adds each value into
Totaland keeps the largest inWettest.
Keywords and builtins used here
HighIntegerLowWriteLnarraybeginconstdoendforifofprogramthentovar
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.
Step 1 of 3 in Arrays & records, step 13 of 27 in Language basics.