! $Id: average_payoff.f90,v 1.7 1997/11/12 02:03:48 svmcguir Exp $
! Scot McGuire 

real*8 function average_payoff(max_params,params,payoff,eta,sortorder)
   use PATHS
   use input_params
   implicit none

   integer,intent(IN)                      ::max_params
   real*8,dimension(max_params),intent(IN) ::params
   real*8,dimension(:,:),intent(IN)        ::payoff
   type (PATH),dimension(:,:),intent(IN)   ::eta
   integer,dimension(:,:),intent(IN)       ::sortorder

   integer                                 ::i,j
	real*8                                  ::time
   
   average_payoff = 0
   do j=1,int(params(P_NPATH))
      if (eta(1,j)%spin == 1) then
         time = 1.0d0*(int(params(P_NTIME))-1)/12.0d0
         average_payoff = average_payoff + payoff(1,j)
      else
         do i=1,int(params(P_NTIME))-1
            time = 1.0d0*(int(params(P_NTIME))-i)/12.0d0
            if (eta(i,j)%spin == 0 .and. eta(i+1,j)%spin == 1) then
               average_payoff=average_payoff+payoff(i+1,j) 
	       exit
	    end if
	 end do
      end if
   end do
   average_payoff = average_payoff / params(P_NPATH)
   return
end function average_payoff

