    Program g01sjfe
!     G01SJF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: g01sjf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: i, ifail, lk, ln, lout, lp
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: p(:), peqk(:), pgtk(:), plek(:)
      Integer, Allocatable             :: ivalid(:), k(:), n(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, mod, repeat
!     .. Executable Statements ..
      Write (nout,*) 'G01SJF Example Program Results'
      Write (nout,*)

!     Skip heading in data file
      Read (nin,*)

!     Read in the input vectors
      Read (nin,*) ln
      Allocate (n(ln))
      Read (nin,*) n(1:ln)

      Read (nin,*) lp
      Allocate (p(lp))
      Read (nin,*) p(1:lp)

      Read (nin,*) lk
      Allocate (k(lk))
      Read (nin,*) k(1:lk)

!     Allocate memory for output
      lout = max(ln,lp,lk)
      Allocate (peqk(lout),pgtk(lout),plek(lout),ivalid(lout))

!     Calculate probability
      ifail = -1
      Call g01sjf(ln,n,lp,p,lk,k,plek,pgtk,peqk,ivalid,ifail)

      If (ifail==0 .Or. ifail==1) Then
!       Display titles
        Write (nout,*) '    N         P         K       PLEK   &
          &   PGTK      PEQK    IVALID'
        Write (nout,*) repeat('-',68)

!       Display results
        Do i = 1, lout
          Write (nout,99999) n(mod(i-1,ln)+1), p(mod(i-1,lp)+1), &
            k(mod(i-1,lk)+1), plek(i), pgtk(i), peqk(i), ivalid(i)
        End Do
      End If

99999 Format (1X,I6,4X,F6.2,4X,I6,3(4X,F6.3),4X,I3)
    End Program g01sjfe
