MagicSquares.ob07 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (*
  2. adapted to Oberon-07 by 0CodErr, KolibriOS team
  3. *)
  4. (* ********* Zonnon online collection ***********
  5. * Magic Squares
  6. *
  7. * This example is a part of Prof. Nikalus Wirth's book
  8. * www.zonnon.ethz.ch/usergroup
  9. * (c) ETH Zurich
  10. *)
  11. MODULE MagicSquares; (*NW 11.8.97*)
  12. IMPORT In, Out, Console;
  13. PROCEDURE Generate; (*magic square of order 3, 5, 7, ... *)
  14. VAR
  15. i, j, x, nx, nsq, n: INTEGER;
  16. M: ARRAY 13, 13 OF INTEGER;
  17. BEGIN
  18. Out.String("Enter magic square order(3, 5, 7, ..., 13): "); In.Int(n); nsq := n * n; x := 0;
  19. i := n DIV 2; j := n - 1;
  20. WHILE x < nsq DO
  21. nx := n + x; j := (j - 1) MOD n; INC(x);
  22. Out.Int(i, 1); Out.Char(9X);
  23. Out.Int(j, 1); Out.Ln;
  24. M[i, j] := x;
  25. WHILE x < nx DO
  26. i := (i + 1) MOD n; j := (j + 1) MOD n;
  27. INC(x); M[i, j] := x
  28. END
  29. END;
  30. FOR i := 0 TO n - 1 DO
  31. FOR j := 0 TO n - 1 DO Out.Int(M[i, j], 6) END;
  32. Out.Ln
  33. END
  34. END Generate;
  35. BEGIN
  36. Console.open;
  37. Generate;
  38. Out.String("Press Enter to continue"); In.Ln;
  39. Console.exit(TRUE)
  40. END MagicSquares.