program gauss_seidel;
uses wincrt;
Const n=3;
var i,j,It:integer;
 type mat= array [1..10, 1..10] of real;
 type vect= array [1..10] of real;
 Var a:mat;
     b,x1,x2,Er:vect;
     S1,S2,R,e,Maxer :real;
 Te:boolean;
 Label 10;
 begin
     writeln('Erreur = ?');
     readln (e); 
      for i:=1 to n do
          begin
            for j:=1 to n do
              begin
                writeln('a[',i,j,'] = ?');
                readln(a[i,j]);
              end;
            writeln('b[',i,'] = ?');
            readln (b[i]);
          end;
          For i:=1 to n do
           Begin
            writeln('X1[',i,'] = ?');
            readln (x1[i]);
           end;
 Te:=false;
 It:=0;
 While Te=false do
       begin
         for i:=1 to n do
            begin
                 S1:=0;
                 S2:=0;
              For j:=i+1 to n do
                 begin
                  S2:=S2-a[i,j]*x1[j];
                 end;
            
              For j:=0 to i-1 do
                 begin
                  S1:=S1-a[i,j]*x2[j];
                 end;
              x2[i]:=(b[i]+S1+S2)/a[i,i];
              Er[i]:=abs(x2[i]-x1[i]);     
            end;
            Maxer:=Er[1];
         For i:=2 to n do
          begin
            If Er[i]>Maxer Then
              Maxer:=Er[i];
          end;
         { Writeln ('Maxer = ', Maxer); }
          It:=It+1;
         If (It>=10000) Or ((It>=100) and (Maxer >=1000000)) Then
            begin
             Writeln('Le système n'' pas de solution ou présente une divergence partique');
             Writeln('www.hajomar.com')
             goto 10;
            end;
         If Maxer<=e then
          Te:=True
         Else
           For i:=1 to n do
             begin
                x1[i]:=x2[i];
             end;
       end;
 Writeln ('La solution du système d''équations :');
 For i:=1 to n do
    begin
      For j:=1 to n-1 do
       Begin
         Write (a[i,j]:3:1,'*');
         Write ('x',j,'+');
       End;
       write(a[i,n]:3:1,'*','x',n,' = ',b[i]:3:1);
      writeln(' ');
    End;
Writeln ( 'Est :');

For i:=1 to n do
begin
writeln('x[',i,'] = ',x2[i]);
End;
Writeln('calculée à une erreur de ',e);
Writeln('Nombre d''itérations = ', It);
10: Writeln('Avec mes meilleurs voeux *** www.hajomar.com ***')
End.
