MODULE reversal



!  Copyright 1994, Miles Ellis, Ivor Philips and Tom Lahey

!  Copyright 1994, Addison-Wesley Publishers Ltd.

!  Copyright 1994, Addison-Wesley Publishing Company Inc.

!  Permission is granted for the use of this code for the purpose of teaching

!  and/or learning the Fortran 90 language provided that the above copyright

!  notices are included in any copies made.

!  Neither the authors nor the publishers accept any responsibility for

!  any results obtained by use of this code.



      CONTAINS

      FUNCTION reverse(array)



!  Copyright 1994, Miles Ellis, Ivor Philips and Tom Lahey

!  Copyright 1994, Addison-Wesley Publishers Ltd.

!  Copyright 1994, Addison-Wesley Publishing Company Inc.

!  Permission is granted for the use of this code for the purpose of teaching

!  and/or learning the Fortran 90 language provided that the above copyright

!  notices are included in any copies made.

!  Neither the authors nor the publishers accept any responsibility for

!  any results obtained by use of this code.



         IMPLICIT NONE



         ! This function reverses the order of the elements of

         ! the array supplied as the argument



         ! Function type and dummy argument

         INTEGER, DIMENSION(:), INTENT(IN) :: array

         INTEGER, DIMENSION(SIZE(array,1)) :: reverse



         ! Create the vector subscript

         INTEGER :: i

         INTEGER, DIMENSION(SIZE(array,1)) :: v_sub

         DO i=1, SIZE(array,1)

            v_sub(i) = SIZE(array,1) - i + 1

         END DO

         reverse = array(v_sub)

      END FUNCTION reverse

END MODULE reversal



      PROGRAM exercise_13_7



!  Copyright 1994, Miles Ellis, Ivor Philips and Tom Lahey

!  Copyright 1994, Addison-Wesley Publishers Ltd.

!  Copyright 1994, Addison-Wesley Publishing Company Inc.

!  Permission is granted for the use of this code for the purpose of teaching

!  and/or learning the Fortran 90 language provided that the above copyright

!  notices are included in any copies made.

!  Neither the authors nor the publishers accept any responsibility for

!  any results obtained by use of this code.



         USE reversal

         IMPLICIT NONE



         ! This program tests the function reverse



         ! Declarations

         INTEGER :: i,j,row

         INTEGER, DIMENSION(6,7) :: array = &

            RESHAPE ((/ ((10*i+j, i=1,6),j=1,7) /),(/ 6,7 /))



         ! Ask which row to reverse

         PRINT *,"Which row is to be reversed (in range 1-7)?"

         READ *,row



         ! Deal with invalid replies

         SELECT CASE (row)

         CASE (:0)

            row = 1

            PRINT *,"Out of range - row 1 will be reversed"

         CASE (8:)

            row = 7

            PRINT *,"Out of range - row 7 will be reversed"

         END SELECT



         ! Use function to reverse the appropriate array section

         array(row,:) = reverse(array(row,:))

         ! Print the array in a rectangular pattern         

         PRINT '(7I6)',((array(i, j),j=1,7),i=1,6)      

      END PROGRAM exercise_13_7



