    PROGRAM e02bbfe

!      E02BBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e02bbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: a, b, s, x
       INTEGER                         :: ifail, j, m, ncap, ncap7, r
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: c(:), lamda(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          real
!      .. Executable Statements ..
       WRITE (nout,*) 'E02BBF Example Program Results'

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

       READ (nin,*) m
       READ (nin,*) ncap
       ncap7 = ncap + 7
       ALLOCATE (lamda(ncap7),c(ncap7))

       READ (nin,*) lamda(1:ncap7)
       READ (nin,*) c(1:ncap+3)

       a = lamda(4)
       b = lamda(ncap+4)

       DO r = 1, m
          x = (real(m-r,kind=nag_wp)*a+real(r-1,kind=nag_wp)*b)/ &
             real(m-1,kind=nag_wp)

          ifail = 0
          CALL e02bbf(ncap7,lamda,c,x,s,ifail)

          IF (r==1) THEN
             WRITE (nout,*)
             WRITE (nout,*) '  J       LAMDA(J)    B-spline coefficient (J-2)'
             WRITE (nout,*)

             DO j = 1, ncap7

                IF (j<3 .OR. j>ncap+5) THEN
                   WRITE (nout,99999) j, lamda(j)
                ELSE
                   WRITE (nout,99999) j, lamda(j), c(j-2)
                END IF

             END DO

             WRITE (nout,*)
             WRITE (nout,*) '  R       Argument      Value of cubic spline'
             WRITE (nout,*)
          END IF

          WRITE (nout,99999) r, x, s
       END DO

99999  FORMAT (1X,I3,F14.4,F21.4)
    END PROGRAM e02bbfe
