--------------------------------------------------------------------------------------------------------------------------------------
PROGRAM keramer
IMPLICIT NONE
INTEGER::i,n,j
REAL::detnet,det
REAL,ALLOCATABLE::a(:,:),b(:),x(:),c(:)
PRINT*
PRINT*
PRINT*," 1 | X(1)a(1,1) + X(2)a(1,2) + ... + X(n)a(1,n) = b(1) |"
PRINT*," 2 | X(1)a(2,1) + X(2)a(2,2) + ... + X(n)a(2,n) = b(2) |"
PRINT*," . | . . . . . . . . . |"
PRINT*," . | . . . . . . . . . |"
PRINT*," . | . . . . . . . . . |"
PRINT*," . | . . . . . . . . . |"
PRINT*," n |_ X(1)a(n,1) + X(2)a(n,2) + ... + X(n)a(n,n) = b(n)_|"
PRINT*," n*n+1"
PRINT*
PRINT*,"-------------------------------------------------------------------"
PRINT*," lotfan n ya tedad moadelat ra vared konid : "
READ*,n
ALLOCATE(a(n,n),b(n),x(n),c(n))
DO i=1,n
PRINT*," satre",i,"matris ra vared konid :"
READ*,a(i,:)
PRINT*," b(",i,") ra vared konid : "
READ*,b(i)
END DO
PRINT*
PRINT*
call deterMINant(n,a,det)
detnet=det
DO j=1,n
c=a(:,j)
PRINT*
a(:,j)=b
call deterMINant(n,a,det)
x(j)=det/detnet
PRINT*," X(",j,") = ",x(j)
a(:,j)=c
END DO
PRINT*
PRINT*,"------------------------------------------------"
PRINT*
END
subroutine deterMINant(n,a,det)
IMPLICIT NONE
INTEGER::i,j,g,f,r,i1,j1,k,s,n
REAL::landa,a(n,n),b(n),det,t,d(n,n)
d=a
k=0
DO i=1,n
IF (a(i,i)==0) THEN
DO j=i+1,n
IF (a(j,i)/=0) THEN
k=k+1
b=a(j,:)
a(j,:)=a(i,:)
a(i,:)=b
END IF
END DO
END IF
DO j1=i+1,n
landa=-a(j1,i)/a(i,i)
a(j1,:)=(landa*a(i,:))+a(j1,:)
END DO
END DO
t=1
DO s=1,n
t=t*a(s,s)
END DO
IF (MOD(k,2)==0) THEN
det=t
ELSE
det=-t
END IF
a=d
END