      program pivot
!
!     Have a function return a pointer to the row containing 
!     the largest element in an array.  This function has
!     application in the Gauss-Jordan elemination technique
!     of solving a set of linear equations.  See discussion
!     (and full program) on pages 24 through 29 of William
!     H. Press et. al., 1986,  NUMERICAL RECEIPES, Cambridge
!     University Press, 818 pages, Cambridge, England.
!
!     Define the function interface.
      interface
        function pivot_row ( a )
          real,    dimension ( :,: ), target  :: a
          real,    dimension (  :  ), pointer :: pivot_row
        end function pivot_row
      end interface
!
!     Declare variables and arrays.
      real, dimension (3,3), target  :: a 
      integer                        :: i, m, n
      real, dimension ( : ), pointer :: p
!
!     Initialize the target array.
      n = size ( a, dim=1 )
      m = n * n
      a = reshape ( (/ (i,i=1,m) /), (/ n,n /) )
!
!     Display the array.
      print *, " "
      print *, "Array ... row 1 : ", a(1,:)
      do i = 2, n, 1
         print *, "      ... row", i, ": ", a(i,:)
      end do
!
!     Pick the row with the largest array element.
      p => pivot_row ( a )
!
!     Display the row with the largest array element.
      print *, " "
      print *, "Pivot row       : ", p
!
!     Display the largest element in this row.
      print *, " "
      print *, "Pivot element   : ", maxval ( p )  
      nullify ( p ) 
!
!     End of program.
      print *, " "
      end program pivot
!
!     Select the row with the largest array element.
      function pivot_row ( a )
      real,    dimension ( :,: ), target  :: a
      real,    dimension (  :  ), pointer :: pivot_row
      integer, dimension (  2  )          :: location
      location   =  maxloc ( a )
      pivot_row  => a(location(1),:)
      end function pivot_row
