    PROGRAM f04lhfe

!      F04LHF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f01lhf, f04lhf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: lena = 200, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: tol
       INTEGER                         :: i, ifail, index, ir, j, k, ldb, n,   &
                                          nbasek, nbloks
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), b(:,:)
       INTEGER, ALLOCATABLE            :: blkstr(:,:), pivot(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F04LHF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) nbloks
       ALLOCATE (a(lena),blkstr(3,nbloks))
       nbasek = 0
       n = 0
       DO i = 1, nbloks
          READ (nin,*) blkstr(1:3,i)
          DO k = 1, blkstr(1,i)
             IF (nbasek+blkstr(2,i)*blkstr(1,i)>lena) THEN
                WRITE (nout,*) ' Array A is too small for this problem'
                GO TO 20
             ELSE
                READ (nin,*) (a(nbasek+(j-1)*blkstr(1,i)+k),j=1,blkstr(2,i))
             END IF
          END DO
          nbasek = nbasek + blkstr(2,i)*blkstr(1,i)
          n = n + blkstr(1,i)
       END DO
       READ (nin,*) ir
       ldb = n
       ALLOCATE (b(ldb,ir),pivot(n))
       tol = 0.0E0_nag_wp

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f01lhf(n,nbloks,blkstr,a,lena,pivot,tol,index,ifail)

       READ (nin,*) (b(1:n,j),j=1,ir)

       ifail = 0
       CALL f04lhf('N',n,nbloks,blkstr,a,lena,pivot,b,ldb,ir,ifail)

       WRITE (nout,*) 'Component Solution'
       DO i = 1, n
          WRITE (nout,99999) i, b(i,1:ir)
       END DO
20     CONTINUE

99999  FORMAT (1X,I5,6X,5F7.4)
    END PROGRAM f04lhfe
