PROGRAM Elem_Rev_SUB
! This program includes a SUBROUTINE which reverses the elements
! of a run-time array.
IMPLICIT NONE
INTEGER :: N
REAL, DIMENSION(:), ALLOCATABLE :: A
! Now read the size of array A. Notice that the indices start at 1.
PRINT *, "Enter size of array A:"
READ *, N
! Now allocate memory for array A:
ALLOCATE(A(N))
! Now read elements of A.
PRINT *, "Enter elements of A. Notice that there are ", N, " of them."
READ *, A
CALL Rev_SUB(A)
! Now we print the reversed array:
PRINT *, A
! Now free memory allocated for A.
! You use DEALLOCATE and ALLOCATE only for run-time arrays.
DEALLOCATE(A)
CONTAINS
SUBROUTINE Rev_SUB(A)
! Reverses elements of array A.
! Input : run-time array A.
! Output : run-time array A.
REAL, DIMENSION(:), INTENT(INOUT) :: A
INTEGER :: I, N
REAL :: Tmp
N=SIZE(A)
! Now we start the reversing process.
! =======================
! Reversing process starts here.
DO I=1, N/2
! We swap A(I) with A(N-I+1)
Tmp=A(I)
A(I)=A(N-I+1)
A(N-I+1)=Tmp
END DO
! Reversing process Ends here.
!=====================
END SUBROUTINE Rev_SUB
END PROGRAM Elem_Rev_SUB