    PROGRAM e04ndfe

!      E04NDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e04ncf, e04ndf, e04nef, nag_wp, x04abf, x04acf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: iset = 1, nin = 5, ninopt = 7,       &
                                          nout = 6
       CHARACTER (*), PARAMETER        :: fname = 'e04ndfe.opt'
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: obj
       INTEGER                         :: i, ifail, inform, iter, lda, ldc,    &
                                          liwork, lwork, m, mode, n, nclin,    &
                                          outchn, sdc
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:), bl(:), bu(:), c(:,:),  &
                                          clamda(:), cvec(:), work(:), x(:)
       INTEGER, ALLOCATABLE            :: istate(:), iwork(:), kx(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,99998) 'E04NDF Example Program Results'
       FLUSH (nout)

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

       READ (nin,*) m, n, nclin
       liwork = n
       ldc = max(1,nclin)
       lda = max(1,m)

       IF (nclin>0) THEN
          sdc = n
       ELSE
          sdc = 1
       END IF

!      This particular example problem is of type QP2, so we allocate
!      A(LDA,N), CVEC(N), B(1) and define LWORK as below

       IF (nclin>0) THEN
          lwork = 2*n**2 + 10*n + 6*nclin
       ELSE
          lwork = 10*n
       END IF

       ALLOCATE (istate(n+nclin),kx(n),iwork(liwork),c(ldc,sdc),bl(n+nclin), &
          bu(n+nclin),cvec(n),x(n),a(lda,n),b(1),clamda(n+nclin),work(lwork))

       READ (nin,*) cvec(1:n)
       READ (nin,*) (a(i,1:n),i=1,m)
       READ (nin,*) (c(i,1:sdc),i=1,nclin)
       READ (nin,*) bl(1:(n+nclin))
       READ (nin,*)
       READ (nin,*) bu(1:(n+nclin))
       READ (nin,*)
       READ (nin,*) x(1:n)

!      Set the unit number for advisory messages to OUTCHN

       outchn = nout
       CALL x04abf(iset,outchn)

!      Set one option using E04NEF

       CALL e04nef(' Problem Type = QP2 ')

!      Open the options file for reading

       mode = 0

       ifail = 0
       CALL x04acf(ninopt,fname,mode,ifail)

!      Read the options file for the remaining options

       CALL e04ndf(ninopt,inform)

       IF (inform/=0) THEN
          WRITE (nout,99999) ' ** E04NDF terminated with INFORM =', inform
          FLUSH (nout)
          GO TO 20
       END IF

!      Solve the problem

       ifail = 0
       CALL e04ncf(m,n,nclin,ldc,lda,c,bl,bu,cvec,istate,kx,x,a,b,iter,obj, &
          clamda,iwork,liwork,work,lwork,ifail)

20     CONTINUE

99999  FORMAT (1X,A,I5)
99998  FORMAT (1X,A)
    END PROGRAM e04ndfe
