    Program g05pzfe

!     G05PZF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: g05kff, g05pzf, nag_wp, x04eaf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: lseed = 1, nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: genid, ifail, ldx, lr, lstate, mode, &
                                          ncol, nrow, subid
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: r(:)
      Integer                          :: seed(lseed)
      Integer, Allocatable             :: state(:), totc(:), totr(:), x(:,:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: sum
!     .. Executable Statements ..
      Write (nout,*) 'G05PZF Example Program Results'
      Write (nout,*)
      Flush (nout)

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

!     Read in the base generator information and seed
      Read (nin,*) genid, subid, seed(1)

!     Initial call to initialiser to get size of STATE array
      lstate = 0
      Allocate (state(lstate))
      ifail = 0
      Call g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!     Reallocate STATE
      Deallocate (state)
      Allocate (state(lstate))

!     Initialize the generator to a repeatable sequence
      ifail = 0
      Call g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!     Read in the problem size
      Read (nin,*) nrow, ncol

      ldx = nrow
      Allocate (totr(nrow),totc(ncol),x(ldx,ncol))

!     Read in row and column totals
      Read (nin,*) totr(1:nrow)
      Read (nin,*) totc(1:ncol)

      lr = sum(totr(1:nrow)) + 5
      Allocate (r(lr))

!     Using a single call to G05PZF, so set up reference vector
!     and generate values in one go
      mode = 2

!     Generate the random table
      ifail = 0
      Call g05pzf(mode,nrow,ncol,totr,totc,r,lr,state,x,ldx,ifail)

!     Display the results
      ifail = 0
      Call x04eaf('General',' ',nrow,ncol,x,ldx,'Random Table',ifail)
      Write (nout,*)
      Write (nout,Fmt=99999) 'Supplied row totals:', totr(1:nrow)
      Write (nout,Fmt=99999) 'Supplied column totals:', totc(1:ncol)
99999 Format (1X,A/(2X,4I10))
    End Program g05pzfe
