--------------------------------------------------------------------------------------------------------------------------------------
PROGRAM gauss_sidel
IMPLICIT NONE
INTEGER::i,j,i1,j1,k,t1,n,j2,k1
REAL::s,e
REAL,ALLOCATABLE::a(:,:),x(:),y(:),t(:)
PRINT*
PRINT*
PRINT*," 1 2 . . . n n+1"
PRINT*," _ - - - - - _ "
PRINT*," 1 | X(1)a(1,1) + X(2)a(1,2) + ... + X(n)a(1,n) = a(1,n+1) |"
PRINT*," 2 | X(1)a(2,1) + X(2)a(2,2) + ... + X(n)a(2,n) = a(2,n+1) |"
PRINT*," . | . . . . . . . . . . . |"
PRINT*," . | . . . . . . . . . . . |"
PRINT*," . | . . . . . . . . . . . |"
PRINT*," . | . . . . . . . . . . . |"
PRINT*," n |_ X(1)a(n,1) + X(2)a(n,2) + ... + X(n)a(n,n) = a(n,n+1)_|"
PRINT*," n*n+1"
PRINT*
PRINT*,"baraye matris n*n+1 bala lotfan n (tedad moadalat) ra vared konid :"
READ*,n
ALLOCATE (a(n,n+1),x(n),y(n),t(n))
DO i1=1,n
PRINT*,"khate",i1,"ra vared konid ( az a(",i1,", 1) ta a(",i1,",",n+1,")) : "
READ*,a(i1,:)
END DO
PRINT*,"nerkh hamgarayi ra vared konid : "
READ*,e
DO j2=1,n
x(j2)=0
y(j2)=0
END DO
k=1
DO
DO i=1,n
s=0
DO j=1,n
IF (j.ne.i) THEN
s=s+x(j)*a(i,j)
END IF
END DO
x(i)=(a(i,n+1)-s)/a(i,i)
END DO
DO t1=1,n
t(t1)=(abs(x(t1)-y(t1)))/ABS(x(t1))
END DO
IF (MAXVAL(t) < e) THEN
EXIT
ELSE
k=k+1
y(1:n)=x(1:n)
END IF
END DO
PRINT*,"javab ha dar tekrar ",k," ba hadse avaliye X(1:n)=0 ::"
PRINT*
DO k1=1,n
PRINT*," X(",k1,") = ",x(k1)
END DO
PRINT*
END