!!------------------------------------------------------------------------
!! $Id: sorted_average_payoff.f90,v 1.9 1997/11/12 02:03:48 svmcguir Exp $
!!------------------------------------------------------------------------

real*8 function sorted_average_payoff(max_params,params,payoff,eta,io)
  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)       ::io

  integer                                 ::i,j

  sorted_average_payoff = 0
  do j=1,int(params(P_NPATH))
     do i=1,int(params(P_NTIME))
        if (eta(i,io(i,j))%spin == 1) then
           sorted_average_payoff = sorted_average_payoff + payoff(i,io(i,j))
           exit 
        end if
     end do
  end do
  sorted_average_payoff = sorted_average_payoff / params(P_NPATH)
  return
end function sorted_average_payoff
