    Program m01zcfe

!     M01ZCF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

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

100   Continue

99999 Format (1X,A,I3)
99998 Format (1X,12F6.1)
    End Program m01zcfe
