home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_01 / HISPEED1.LZH / DOSDEMO / MAGIC.PAS < prev    next >
Pascal/Delphi Source File  |  1991-07-02  |  1KB  |  53 lines

  1. {$M 5,1,1,10}
  2. {Show a magic square, where the sum of each
  3. coloum, row or diagonal is the same
  4. }
  5.  
  6. Program magic(input,output);
  7.  
  8. const
  9.   maxsize = 19;
  10.  
  11. type
  12.   sqrtype = array[1..maxsize,1..maxsize] of integer;
  13.  
  14. var
  15.   sqare: sqrtype;
  16.   Size,row,col: integer;
  17.  
  18. procedure makesqare(var sq:sqrtype;limit:integer);
  19. var
  20.   num,r,c: integer;
  21. begin
  22.   for r:=1 to limit do for c:=1 to limit do sq[r,c]:=0;
  23.   r:=(limit+1) div 2;
  24.   c:=limit;
  25.   for num:=1 to sqr(limit) do begin
  26.     if sq[r,c]<>0 then begin
  27.       r:=r-1; if r<1 then r:=r+limit;
  28.       c:=c-2; if c<1 then c:=c+limit
  29.     end;
  30.     sq[r,c]:=num;
  31.     gotoxy(r*4,2+c); write(num:4);
  32.     r:=r+1; if r>limit then r:=r-limit;
  33.     c:=c+1; if c>limit then c:=c-limit
  34.   end
  35. end;
  36.  
  37. begin
  38.   Size:=5;
  39.   repeat
  40.     if ((Size>2) and (Size<maxsize+1)) and (odd(Size)) then begin
  41.       ClrScr;
  42.       makesqare(sqare,Size);
  43.       writeln;
  44.     end else begin
  45.        write('Only odd numbers in the range 3..',maxsize:0)
  46.     end;
  47.     gotoxy(1,22);
  48.     ClrEol;
  49.     write('Enter size (0 to exit) : '); read(Size);
  50.     Writeln;
  51.   until Size=0;
  52. end.
  53.