uses
Math;
type
TData = array of array of Integer;
procedure FillData(var a: TData;
aWidth, aDepth: Integer);
var
LeftX, RightX, TopY, BottomY, Value: Integer;
procedure SetBounds(aLeft, aTop, aRight, aBottom: Integer);
begin
LeftX := aLeft;
TopY := aTop;
RightX := aRight;
BottomY := aBottom;
end;
function NextValue: Integer;
begin
Inc(Value);
Result := Value;
end;
var
X, Y, i, MinSide: Integer;
begin
if (aWidth < 0) or (aDepth < 0) then
Exit;
Value := 0;
SetLength(a, aDepth, aWidth);
SetBounds(0, 0, aWidth - 1, aDepth - 1);
MinSide := Min(aWidth, aDepth);
for i := 1 to MinSide div 2do
begin
for X := LeftX to RightXdo
a[TopY, X] := NextValue;
for Y := TopY + 1 to BottomYdo
a[Y, RightX] := NextValue;
for X := RightX - 1do
wnto LeftXdo
a[BottomY, X] := NextValue;
for Y := BottomY - 1do
wnto TopY + 1do
a[Y, LeftX] := NextValue;
SetBounds(LeftX + 1, TopY + 1, RightX - 1, BottomY - 1);
end;
if Odd(MinSide) then
begin
for X := LeftX to RightXdo
a[TopY, X] := NextValue;
for Y := TopY + 1 to BottomYdo
a[Y, RightX] := NextValue;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
cWidth: Integer = 5;
cDepth: Integer = 6;
var
B: TData;
i, j: Integer;
TempStr: String;
begin
FillData(B, cWidth, cDepth);
for j := 0 to cDepth - 1do
begin
TempStr := '';
for i := 0 to cWidth - 1do
TempStr := TempStr + Format('%3d',[B[j, i]]);
Memo1.Lines.Add(TempStr);
end;
end;