SpiralMatrix.ob07 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. (*
  2. adapted to Oberon-07 by 0CodErr, KolibriOS team
  3. *)
  4. (*
  5. Produce a spiral array.
  6. A spiral array is a square arrangement of the first (Width * Height) natural numbers,
  7. where the numbers increase sequentially as you go around the edges of the array spiraling inwards.
  8. *)
  9. MODULE SpiralMatrix;
  10. IMPORT In, Out, Console;
  11. VAR
  12. Width, Height: INTEGER;
  13. PROCEDURE spiral(w, h, x, y: INTEGER): INTEGER;
  14. VAR
  15. res: INTEGER;
  16. BEGIN
  17. IF y # 0 THEN
  18. res := w + spiral(h - 1, w, y - 1, w - x - 1)
  19. ELSE
  20. res := x
  21. END
  22. RETURN res
  23. END spiral;
  24. PROCEDURE print_spiral(w, h: INTEGER);
  25. VAR
  26. i, j: INTEGER;
  27. BEGIN
  28. FOR i := 0 TO h - 1 DO
  29. FOR j := 0 TO w - 1 DO
  30. Out.Int(spiral(w, h, j, i), 4)
  31. END;
  32. Out.Ln
  33. END
  34. END print_spiral;
  35. BEGIN
  36. Console.open;
  37. Out.String("Input width of matrix(1, 2, 3, ...):"); In.Int(Width);
  38. Out.String("Input height of matrix:(1, 2, 3, ...)"); In.Int(Height);
  39. print_spiral(Width, Height);
  40. In.Ln;
  41. Console.exit(TRUE)
  42. END SpiralMatrix.