uses crt;
type mt=array[1..5,1..5] of real;
mang=array[1..5] of real;
function chuan1(b:mt;n:integer):real;
var i,j:integer;s:real;
begin
i:=1;
s:=0;
while (i<=n) and (s<1) do
begin
s:=0;
for j:=1 to n do
s:=s+abs(b[i,j]);
i:=i+1;
end;
{if i>n then chuan1:=true
else chuan1:=false;}
chuan1:=s;
end;
Var a,b:mt;f,g,x,y:mang;n,i,j,m,k,l,h:integer;
D,ss:real;kt,ktra,co:boolean;
Begin
clrscr;
textmode(co80);
repeat
write('Giai he bao nhieu phuong trinh:');
readln(n);
until (n>=2) and (n<=5);
writeln('Nhap ma tran cac he so:');
nhapmt(a,n);
writeln('Nhap cac he so tu do:');
for i:=1 to n do
begin
write('Nhap f[',i,']=');
readln(f[i]);
end;
writeln('Ma tran cac he so vua nhap la:');
xuatmt(a,n);
writeln('Ma tran cac he so tu do:');
for i:=1 to n do
writeln(f[i]:8:3);
D:=dtcn(a,n);
writeln('D=',d:0:4);
if D<>0 then
begin
for i:=1 to n do
for j:=1 to n do
if i=j then b[i,j]:=0
else b[i,j]:=-a[i,j]/a[i,i];
for i:=1 to n do
g[i]:=f[i]/a[i,i];
writeln('Ma tran tuong duong B :');
xuatmt(b,n);
writeln('Ma tran cac he so G:');
for i:=1 to n do
writeln(g[i]:8:3);
kt:=false;
if chuan1(b,n)<1 then kt:=true
writeln('chuan 1 = ',chuan1(b,n):0:4);
if kt=true then
begin
write('Nhap sai so:');
readln(ss);
for i:=1 to n do
x[i]:=0;
ktra:=false;
while ktra=false do
begin
for j:=1 to n do
begin
y[j]:=0;d:=0;
for k:=1 to n do
y[j]:=y[j]+b[j,k]*x[k];
y[j]:=y[j]+g[j];
if abs(y[j]-x[j])>ss then d:=d+1;
x[j]:=y[j];
end;
if d=0 then ktra:=true;
for i:=1 to n do
writeln('x',i,'=',x[i]:0:4);
readln;
end;
writeln('Nghiem cua he voi sai so ',ss:0:4,' la : ');
for i:=1 to n do
writeln('x',i,'=',x[i]:0:4);
end
else writeln('Khong hoi tu');
end;
readln;
End.