Я сделал так...
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure exit;
begin
ShowMessage('Не магический квадрат!');
halt;
end;
procedure TForm1.FormCreate(Sender: TObject);
const n=3;
var a:array[1..n,1..n]of integer;
i,j:byte;
s,s1:integer;
begin
randomize;
for i:=1 to n do
for j:=1 to n do
begin
a[i,j]:=1{random(10)};
Form1.StringGrid1.cells[i-1,j-1]:=IntToStr(a[i,j]);
end;
s:=0;
s1:=0;
for i:=1 to n do
s:=a[i,i]+s;
for i:=1 to n do
begin
for j:=1 to n do
s1:=s1+a[i,j];
if s1<>s then exit;
s1:=0;
end;
for i:=1 to n do
begin
for j:=n downto 1 do
s1:=s1+a[i,j];
if s1<>s then exit;
s1:=0;
end;
for j:=1 to n do
begin
for i:=1 to n do
s1:=s1+a[i,j];
if s1<>s then exit;
s1:=0;
end;
ShowMessage('Магический квадрат!');
end;
end.