    PROGRAM f11zbfe

!      F11ZBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11zbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, n, nnz
       CHARACTER (1)                   :: dup, zer
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:)
       INTEGER, ALLOCATABLE            :: icol(:), irow(:), istr(:), iwork(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F11ZBF Example Program Results'
!      Skip heading in data file
       READ (nin,*)

!      Read order of matrix and number of non-zero entries

       READ (nin,*) n
       READ (nin,*) nnz

       ALLOCATE (a(nnz),icol(nnz),irow(nnz),istr(n+1),iwork(n))

!      Read and output the original non-zero elements

       DO i = 1, nnz
          READ (nin,*) a(i), irow(i), icol(i)
       END DO
       WRITE (nout,*) 'Original elements'
       WRITE (nout,99997) 'NNZ = ', nnz
       DO i = 1, nnz
          WRITE (nout,99998) i, a(i), irow(i), icol(i)
       END DO

!      Reorder, sum duplicates and remove zeros

       dup = 'S'
       zer = 'R'

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f11zbf(n,nnz,a,irow,icol,dup,zer,istr,iwork,ifail)

!      Output results

       WRITE (nout,*) 'Reordered elements'
       WRITE (nout,99999) 'NNZ = ', nnz
       DO i = 1, nnz
          WRITE (nout,99998) i, a(i), irow(i), icol(i)
       END DO

99999  FORMAT (1X,A,I4)
99998  FORMAT (1X,I8,E16.4,2I8)
99997  FORMAT (1X,A,I16)
    END PROGRAM f11zbfe
