      program deter1dr

      REAL A(50,50),D
      integer n
      write (*,*)"Kerem a matrix sorainak a szamat:"
      read(*,*)n

      do i=1,n
	 do j=1,n
         write(*,*)"A matrix (",i,",",j,")-dik eleme :"
	 read(*,*)a(i,j)
         enddo
      enddo

      call DTRM (A,N,D)
      
      write(*,*)D

      stop
      end




      SUBROUTINE DTRM (A,N,D)

cccccc Subroutine matrix determinansanak a kiszamitasara.


      INTEGER N,I,J,MSGN,INDX(50)
      REAL A(50,50),D
      LOGICAL IR

      IR=.TRUE.
      CALL ELGS(A,N,IR,INDX)
      D=1.0
      DO I=1,N
	D=D*A(INDX(I),I)
      END DO
      MSGN=1
      DO I=1,N
	DO WHILE(I.NE.INDX(I))
	  MSGN=-MSGN
	  J=INDX(I)
	  INDX(I)=INDX(J)
	  INDX(J)=J
	END DO
      END DO
      D=MSGN*D
      RETURN
      END



      SUBROUTINE ELGS (A,N,IR,INDX)

cccccc Subroutine Gauss eliminaciora.

      IMPLICIT NONE
      INTEGER N,I,J,K,ITMP,INDX(50)
      REAL C1,PI,PI1,PJ,A(50,50),C(50)
      LOGICAL IR

      DO I=1,N
	INDX(I)=I
      END DO

      DO I=1,N
	C1=0.0
	DO J=1,N
	  C1=AMAX1(C1,ABS(A(I,J)))
	END DO
	C(I)=C1
      END DO

      DO I=1,N
	DO J=1,N
	  A(I,J)=A(I,J)/C(I)
	END DO
      END DO

      DO J=1,N-1
	PI1=0.0
	DO I=J,N
	  PI=ABS(A(INDX(I),J))
	  IF(PI.GT.PI1) THEN
	    PI1=PI
	    K=I
	  ENDIF
	END DO

	ITMP=INDX(J)
	INDX(J)=INDX(K)
	INDX(K)=ITMP
	DO I=J+1,N
	  PJ=A(INDX(I),J)/A(INDX(J),J)
	  A(INDX(I),J)=PJ
	  DO K=J+1,N
	    A(INDX(I),K)=A(INDX(I),K)-PJ*A(INDX(J),K)
	  END DO
	END DO
      END DO

      IF (IR) THEN
	DO I=1,N
	  DO J=1,N
	    A(I,J)=C(I)*A(I,J)
	  END DO
	END DO
      END IF
      RETURN
      END

