    Program g02aafe

!     G02AAF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. 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
