Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  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.  
  10. MODULE SpiralMatrix;
  11.  
  12. IMPORT In, Out, Console;
  13.  
  14.  
  15. VAR
  16.     Width, Height: INTEGER;
  17.  
  18.  
  19. PROCEDURE spiral(w, h, x, y: INTEGER): INTEGER;
  20. VAR
  21.     res: INTEGER;
  22.  
  23. BEGIN
  24.     IF y # 0 THEN
  25.         res := w + spiral(h - 1, w, y - 1, w - x - 1)
  26.     ELSE
  27.         res := x
  28.     END
  29.     RETURN res
  30. END spiral;
  31.  
  32.  
  33. PROCEDURE print_spiral(w, h: INTEGER);
  34. VAR
  35.     i, j: INTEGER;
  36.  
  37. BEGIN
  38.     FOR i := 0 TO h - 1 DO
  39.         FOR j := 0 TO w - 1 DO
  40.             Out.Int(spiral(w, h, j, i), 4)
  41.         END;
  42.         Out.Ln
  43.     END
  44. END print_spiral;
  45.  
  46.  
  47. BEGIN
  48.     Console.open;
  49.  
  50.     Out.String("Input width of matrix(1, 2, 3, ...):");  In.Int(Width);
  51.     Out.String("Input height of matrix:(1, 2, 3, ...)"); In.Int(Height);
  52.     print_spiral(Width, Height);
  53.     In.Ln;
  54.  
  55.     Console.exit(TRUE)
  56. END SpiralMatrix.