typestar

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

  1. Rainfall: array[0..4] of Integer = (4, 12, 0, 9, 21); fija los límites en compilación.
  2. for I := Low(Rainfall) to High(Rainfall) do toma su rango del propio arreglo.
  3. El cuerpo suma cada valor en Total y guarda el mayor en Wettest.

Palabras clave y builtins usados aquí

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.

Escribe este fragmento

Paso 1 de 3 en Arreglos y registros; paso 13 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →