kolibrios-fun/programs/develop/oberon07/Samples/Windows/Console/SpiralMatrix.ob07
maxcodehack 2f54c7de00 Update oberon07 from akron1's github
git-svn-id: svn://kolibrios.org@8097 a494cfbc-eb01-0410-851d-a64ba20cac60
2020-10-13 07:58:51 +00:00

56 lines
1.1 KiB
Plaintext

(*
adapted to Oberon-07 by 0CodErr, KolibriOS team
*)
(*
Produce a spiral array.
A spiral array is a square arrangement of the first (Width * Height) natural numbers,
where the numbers increase sequentially as you go around the edges of the array spiraling inwards.
*)
MODULE SpiralMatrix;
IMPORT In, Out, Console;
VAR
Width, Height: INTEGER;
PROCEDURE spiral(w, h, x, y: INTEGER): INTEGER;
VAR
res: INTEGER;
BEGIN
IF y # 0 THEN
res := w + spiral(h - 1, w, y - 1, w - x - 1)
ELSE
res := x
END
RETURN res
END spiral;
PROCEDURE print_spiral(w, h: INTEGER);
VAR
i, j: INTEGER;
BEGIN
FOR i := 0 TO h - 1 DO
FOR j := 0 TO w - 1 DO
Out.Int(spiral(w, h, j, i), 4)
END;
Out.Ln
END
END print_spiral;
BEGIN
Console.open;
Out.String("Input width of matrix(1, 2, 3, ...):"); In.Int(Width);
Out.String("Input height of matrix:(1, 2, 3, ...)"); In.Int(Height);
print_spiral(Width, Height);
In.Ln;
Console.exit(TRUE)
END SpiralMatrix.