    PROGRAM g05pzfe

!      G05PZF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. 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 Functions ..
       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,*) 'Supplied row totals    = ', totr(1:nrow)
       WRITE (nout,*) 'Supplied column totals = ', totc(1:ncol)

    END PROGRAM g05pzfe
