I developed the code below for performing gaussian elimination in order to evaluate the determinant of a matrix:

program det3

REAL, DIMENSION(3,3) :: matriz = RESHAPE([1,2,3,-7,13,5,4,-21,8],[3,3])
REAL :: temp, m
INTEGER :: n = 3
INTEGER :: i, j, k
INTEGER :: fator_cor = 1
REAL :: det

DO k=1, n-1

    DO i = k+1, n

                IF (matriz(k,k) == 0) THEN
                    linha_aux = matriz(k,0:n)
                    matriz(k,0:n)= matriz(k+1, 0:n)
                    matriz(k+1,0:n)= linha_aux
                    fator_cor = fator_cor*(-1)

                END IF

    END DO


    DO j = k+1, n
            m = matriz(j,k)/matriz(k,k)

            DO i = k+1, n
                matriz(j,i) = matriz(j,i) - m*matriz(k,i)

            END DO

    END DO

det = l
    DO i = 1, n
        det = det * matriz(i,i)
        det = det*fator_cor
    END DO

WRITE (*,*) det

end program

I'm trying to apply it to the 3x3 matrix declared in the beginning but I'm getting the error:

"Error: Incompatible ranks 0 and 1 in assignment at (1)" linha_aux = matriz(k,0:n)"

I'm a bit new to fortran so I'm not sure what it is refering to. I'm unsure if I should declare linha_aux at all, and if so if I should declare it as an allocatable array. Any help is greatly appreciated.

  • $\begingroup$ Note question would be more suitable for stack overflow. $\endgroup$
    – albert
    Mar 25, 2019 at 8:13

1 Answer 1


Some (bit to to long for a comment) initial remarks:

You try to assign an array matriz(k,0:n) to a scalar integer variable linha_aux. linha_aux is a scalar integer variable as it is implicitly declared.

  • declare all variables
  • use IMPLICIT NONE so you have to declare all variables
  • Fortran starts counting elements at 1, so matriz(k,0:n) has a problem as n is declared as 3 and the matrix has size 3 times 3.
  • the nested loops:

    DO k=1, n-1
       DO i = k+1, n 

    as the content of the loop does not use i and also in matriz(k+1,0:n)the matrix would go out of bounds on the first index (and and the second due to the 0).

  • note that Fortran has column major order (so the first index runs fasts), this ordering is other than e.g. C that has row major ordering.


Not the answer you're looking for? Browse other questions tagged or ask your own question.