    PROGRAM g05qdfe

!      G05QDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g05kbf, g05qdf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: liw = 10, ncol = 3, nout = 6,        &
                                          nr = 60, nrow = 4
       INTEGER, PARAMETER              :: ldx = nrow
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, igen, j, rctot
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: r(nr)
       INTEGER                         :: iseed(4), iw(liw), totc(ncol),       &
                                          totr(nrow), x(ldx,ncol)
!      .. Executable Statements ..
       WRITE (nout,*) 'G05QDF Example Program Results'
       WRITE (nout,*)
!      Set the table row and column totals
       totr(1) = 9
       totr(2) = 11
       totr(3) = 7
       totr(4) = 23
       totc(1) = 16
       totc(2) = 17
       totc(3) = 17
       rctot = 50
!      Initialize the seed to a repeatable sequence
       iseed(1) = 1762543
       iseed(2) = 9324783
       iseed(3) = 42344
       iseed(4) = 742355
!      IGEN identifies the stream.
       igen = 1
       CALL g05kbf(igen,iseed)
!      Choose MODE = 2
       ifail = 1
       CALL g05qdf(2,nrow,ncol,totr,totc,x,ldx,igen,iseed,r,nr,iw,liw,ifail)

       IF (ifail==0) THEN
          DO i = 1, nrow
             WRITE (nout,99999) (x(i,j),j=1,ncol), totr(i)
          END DO
          WRITE (nout,*) ' --------------+-------'
          WRITE (nout,99999) (totc(j),j=1,ncol), rctot
       ELSE
          WRITE (nout,99998) ifail
       END IF

99999  FORMAT (1X,3(I4,1X),'|',1X,I5)
99998  FORMAT (1X,' ** G05QDF returned with IFAIL = ',I5)
    END PROGRAM g05qdfe
