Arreglos estáticos en Delphi
Cinco cifras de lluvia en un array[0..4] of Integer, recorridas una vez por un total y un pico.
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.
Cómo funciona
Rainfall: array[0..4] of Integer = (4, 12, 0, 9, 21);fija los límites en compilación.for I := Low(Rainfall) to High(Rainfall) dotoma su rango del propio arreglo.- El cuerpo suma cada valor en
Totaly guarda el mayor enWettest.
Palabras clave y builtins usados aquí
HighIntegerLowWriteLnarraybeginconstdoendforifofprogramthentovar
El intento, en números
- Líneas
- 19
- Caracteres a escribir
- 507
- Tokens
- 119
- Ritmo de tres estrellas
- 75 tpm
Al ritmo de tres estrellas de 75 tokens por minuto, este intento toma unos 95 segundos.
Paso 1 de 3 en Arreglos y registros; paso 13 de 27 en Fundamentos del lenguaje.