    PROGRAM m01nafe

!      M01NAF Example Program Test

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : m01naf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: item
       INTEGER                         :: i, ifail, index, ioerr, m1, m2
       LOGICAL                         :: first
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: rv(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'M01NAF Example Program Results'

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

       READ (nin,*) m2
       ALLOCATE (rv(m2))

       m1 = 1

       READ (nin,*) (rv(i),i=m1,m2)

       first = .TRUE.

DATA:  DO
          READ (nin,*,IOSTAT=ioerr) item

          IF (ioerr<0) THEN
             EXIT DATA
          END IF

          ifail = 0
          index = m01naf(first,rv,m1,m2,item,ifail)

          IF (first) THEN
             WRITE (nout,*)
             WRITE (nout,*) 'Reference Vector is:'
             WRITE (nout,99999) (rv(i),i=m1,m2)
             first = .FALSE.
          END IF

          WRITE (nout,*)
          WRITE (nout,99998) item, index
       END DO DATA

99999  FORMAT (1X,8F7.1)
99998  FORMAT (1X,'Search for item ',F7.1,' returned index: ',I4)
    END PROGRAM m01nafe
