SHARE
TWEET

magic.pp

finalshare Jul 28th, 2016 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.     This file is part of the Free Pascal run time library.
  3.     Copyright (c) 1993-98 by Florian Klaempfl
  4.  
  5.     Magic Square Example
  6.  
  7.     See the file COPYING.FPC, included in this distribution,
  8.     for details about the copyright.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.  
  14.  **********************************************************************}
  15. program magic;
  16.  
  17. {
  18.   Calculate a magic square (sum of the row, colums and diagonals is equal
  19. }
  20.  
  21.   const
  22.      maxsize = 11;
  23.  
  24.   type
  25.      sqrtype = array[1..maxsize, 1..maxsize] of longint;
  26.  
  27.   var
  28.      square : sqrtype;
  29.      size, row, sum : longint;
  30.  
  31.   procedure makesquare(var sq : sqrtype;limit : longint);
  32.  
  33.     var
  34.        num,r,c : longint;
  35.  
  36.     begin
  37.        for r:=1 to limit do
  38.          for c:=1 to limit do
  39.            sq[r, c] := 0;
  40.        if (limit and 1)<>0 then
  41.          begin
  42.             r:=(limit+1) div 2;
  43.             c:=limit;
  44.             for num:=1 to limit*limit do
  45.               begin
  46.                  if sq[r,c]<>0 then
  47.                    begin
  48.                       dec(r);
  49.                       if r<1 then
  50.                         inc(r,limit);
  51.                       dec(c,2);
  52.                       if c<1 then
  53.                         inc(c,limit);
  54.                    end;
  55.                  sq[r,c]:=num;
  56.                  inc(r);
  57.                  if r>limit then
  58.                    dec(r,limit);
  59.                  inc(c);
  60.                  if c>limit then
  61.                    dec(c,limit);
  62.               end;
  63.          end;
  64.      end;
  65.  
  66.   procedure writesquare(var sq : sqrtype;limit : longint);
  67.  
  68.     var
  69.        row,col : longint;
  70.  
  71.     begin
  72.        for row:=1 to Limit do
  73.          begin
  74.             for col:=1 to (limit div 2) do
  75.               write(sq[row,2*col-1]:4,' ',sq[row,2*col]:4,' ');
  76.             writeln(sq[row,limit]:4);
  77.          end;
  78.     end;
  79.  
  80. begin
  81.   size:=3;
  82.   while (size<=maxsize) do
  83.     begin
  84.        writeln('Magic Square with size ',size);
  85.        writeln;
  86.        makesquare(square,size);
  87.        writesquare(square,size);
  88.        writeln;
  89.        sum:=0;
  90.        for row:=1 to size do
  91.          inc(sum,square[row,1]);
  92.        writeln('Sum of the rows,columns and diagonals = ', sum);
  93.        writeln;
  94.        writeln;
  95.        inc(size,2);
  96.     end;
  97. end.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top