    PROGRAM e04ncfe

!      E04NCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e04ncf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: obj
       INTEGER                         :: i, ifail, iter, lda, ldc, liwork,    &
                                          lwork, m, n, nclin, 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,*) 'E04NCF 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 LS1, so we allocate
!      A(LDA,N), CVEC(1), B(M) and define LWORK as below

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

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

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

!      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)

    END PROGRAM e04ncfe
