    PROGRAM m01zcfe

!      M01ZCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : m01djf, m01zcf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: t
       INTEGER                         :: i, ifail, ii, j, k, l, ldm, m1, m2,  &
                                          n1, n2
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: rm(:,:)
       INTEGER, ALLOCATABLE            :: icycl(:), iperm(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'M01ZCF Example Program Results'

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

       READ (nin,*) m2, n2, l

       IF (l<1 .OR. l>m2) THEN
          GO TO 20
       END IF

       ldm = m2
       ALLOCATE (rm(ldm,n2),icycl(n2),iperm(n2))

       m1 = 1
       n1 = 1

       DO i = m1, m2
          READ (nin,*) (rm(i,j),j=n1,n2)
       END DO

       ifail = 0
       CALL m01djf(rm,ldm,l,l,n1,n2,'Ascending',iperm,ifail)

       ifail = 0
       CALL m01zcf(iperm,n1,n2,icycl,ifail)

       DO k = n1, n2
          i = icycl(k)

          IF (i<0) THEN
             j = -i
          ELSE

!            Swap columns I and J

             DO ii = m1, m2
                t = rm(ii,j)
                rm(ii,j) = rm(ii,i)
                rm(ii,i) = t
             END DO

          END IF

       END DO

       WRITE (nout,*)
       WRITE (nout,99999) 'Matrix sorted on row', l
       WRITE (nout,*)

       DO i = m1, m2
          WRITE (nout,99998) (rm(i,j),j=n1,n2)
       END DO

20     CONTINUE

99999  FORMAT (1X,A,I3)
99998  FORMAT (1X,12F6.1)
    END PROGRAM m01zcfe
