Comp Sci Fortran90 SIGSEGV: Segmentation fault

Click For Summary
The user is encountering a segmentation fault (SIGSEGV) when running a Fortran 90 program designed to calculate the determinant of a matrix using Laplace's method, specifically after adding a function to reorganize the matrix based on the number of zeroes in its rows and columns. The error occurs with matrices of size 4x4 and larger, while smaller matrices work correctly. The function attempts to find the row or column with the most zeroes and rearranges the matrix accordingly, but issues arise during this process. The user is seeking guidance on potential mistakes in their code, as they are new to programming and find existing solutions too complex. Clarification on memory management and array indexing in Fortran may be necessary to resolve the segmentation fault.
Ha1234
Messages
1
Reaction score
0
Hi,
I have a project for a class in numerics, where I have to write a program with FORTRAN 90 that calculates the determinant of a matrix using Laplace.
The program itself works now, but I have added a function that finds the row/column with most zeroes and reorganizes the matrix, so that they are in the first column. When I try it with small matrices, it works just fine. However (starting at 4x4) I get the following error:

SIGSEGV:Segmentation fault-invalid memory reference.
Backtrace for this error:
#0 6f6117be
#1 6f695deb
#2 004010f8

I have read pretty much every discussion about this I could find, but either the solutions did not apply to my code or the explanation was too complicated for me to unterstand...This is the first program ever that I wrote, so I am not an expert.

This is the function (whithout this function it works fine, so I gues the problem is somewhere in the function):
Fortran:
      function sortieren_Matrix(A,n) result(A_getauscht)

      implicit none

       !Festlegen der Variablen
         real                     A(n,n)                    !Matrix A
         real                     A_getauscht(n,n)        !Matrix mit vertauschten Zeilen und Spalten
         real                    merk                     !Hilfsvariablen
         integer                    n                         !Größe der Matrix
         integer                    zaehl                    !Zählt die Nullen
         integer                    merk1,merk2                !Hilfsvariablen
         integer                    zaehl_max1,zaehl_max2    !Anzahl Nullen (1-Zeile,2-Spalte)
         integer                    minus                    !negatives Vorzeichen
         integer                 i,j                        !Laufindices
   
    !Durchsuchen nach Zeile mit den meisten Nullen
   
         zaehl_max1=0.0
   
         do i=1,n
              zaehl=0.0
               do j=1,n
                   if(A(i,j) .eq. 0.0) then
                       zaehl=zaehl+1
                  end if
               end do
              if(zaehl .gt. zaehl_max1) then
               zaehl_max1=zaehl
               merk1=i                                     !Merken, in welcher Zeile die meisten Nullen sind
              end if
        end do
   
    !Durchsuchen nach Spalte mit den meisten Nullen
   
        zaehl_max2=0.0
   
         do i=1,n
            zaehl=0.0
                do j=1,n
                    if(A(j,i) .eq. 0.0) then
                        zaehl=zaehl+1
                    end if
                end do
            if(zaehl .gt. zaehl_max2) then
                zaehl_max2=zaehl
                merk2=i                                     !Merken, in welcher Spalte die meisten Nullen sind
            end if       
         end do
   
    !neu Ordnen der Matrix, sodass die erste Spalte, die mit den meisten Nullen ist
   
         if(zaehl_max1 .gt. zaehl_max2) then        !sehen ob in Spalte oder Zeile die meisten Nullen sind
            do i=1,n                                !Zeilen und Spalten vertauschen/transponieren
                do j=1,n
                    A_getauscht(i,j)=A(j,i)
                end do
            end do
            merk2=merk1
        else
            A_getauscht=A 
         end if
   
    !Vorzeichen der Determinante positiv oder negativ?
   
    minus=1
   
         if(merk2 .ne. 1) then            !Wenn bei einer Matrix Spalten oder Zeilen getauscht werden, ändert sich ihr Vrozeichen
          minus=-1
         end if
   
    !Ordnen, sodass Nullen in erster Spalte
   
         do i=1,n
            merk=A_getauscht(i,1)
             A_getauscht(i,1)=A_getauscht(i,merk2)
            A_getauscht(i,merk2)=merk
         end do
   
      end function sortieren_Matrix

I would really apprecaite it if someone had ideas what else I could be doing wrong!
Thank you!
 
Physics news on Phys.org

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 4 ·
Replies
4
Views
2K