    PROGRAM g02aafe

!      G02AAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02aaf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: errtol, nrmgrd
       INTEGER                         :: feval, i, ifail, iter, ldg, ldx,     &
                                          maxit, maxits, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: g(:,:), x(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02AAF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read in the problem size
       READ (nin,*) n

       ldg = n
       ldx = n
       ALLOCATE (g(ldg,n),x(ldx,n))

!      Read in the matrix G
       READ (nin,*) (g(i,1:n),i=1,n)

!      Use the defaults for ERRTOL, MAXITS and MAXIT
       errtol = 0.0E0_nag_wp
       maxits = 0
       maxit = 0

!      Calculate nearest correlation matrix
       ifail = 0
       CALL g02aaf(g,ldg,n,errtol,maxits,maxit,x,ldx,iter,feval,nrmgrd,ifail)

!      Display results
       ifail = 0
       CALL x04caf('General',' ',n,n,x,ldx,'Nearest Correlation Matrix',ifail)
       WRITE (nout,*)
       WRITE (nout,99999) ' Number of Newton steps taken:', iter
       WRITE (nout,99998) ' Number of function evaluations:', feval

99999  FORMAT (1X,A,I11)
99998  FORMAT (1X,A,I9)
    END PROGRAM g02aafe
