module eliminacao

use dados

contains

!----------------------------------------------------------

subroutine el_gauss

	integer :: i, j, p

	real*8  :: EB, m, soma

	real*8, dimension(:), allocatable :: EA

	allocate ( EA(N) )

	! Passo 1
	do i = 1, N-1

		! Passo 2
		do p = i, N

			if ( A(p,i) /= 0 )  exit

		end do

		! Passo 3

		if ( p /= i ) then

			EA(:)  = A(p,:); EB   = b(p)
			A(p,:) = A(i,:); b(p) = b(i)
			A(i,:) = EA(:) ; b(i) = EB

		end if

		! Passo 4
		do j = i+1, N

			! Passo 5
			m = A(j,i)/A(i,i)

			! Passo 6
			A(j,:) = A(j,:) - m*A(i,:)
			b(j)   = b(j)   - m*b(i)

		end do

	end do

	! Passo 7
	if ( A(N,N) == 0.0d0) then
		write(*,*) "Nao existe solucao unica"
		stop
	end if

	! Passo 8
	x(N) = b(N) / A(N,N)

	! Passo 9
	do i = N-1, 1, -1

		soma = 0.0d0
		do j = i+1, N

			soma = soma + A(i,j)*x(j)

		end do

		x(i) = ( b(i) - soma)/A(i,i)

	end do

end subroutine el_gauss

!----------------------------------------------------------

end module eliminacao