Reversing an Array

Problem Statement

Write a program that reverses the order of the elements of a given array. For example, if the given array contains five elements:
3 5 7 2 4
after reversing the order of elements, the result is
4 2 7 5 3

Solution

! ---------------------------------------------------------------
! PROGRAM  Reverse:
!    This program reverses the order of an input array.
! ---------------------------------------------------------------

PROGRAM  Reverse
   IMPLICIT  NONE

   INTEGER, PARAMETER :: SIZE = 30      ! maximum array size
   INTEGER, DIMENSION(1:SIZE) :: a      ! input array
   INTEGER            :: n              ! actual input array size
   INTEGER            :: Head           ! pointer moving forward
   INTEGER            :: Tail           ! pointer moving backward
   INTEGER            :: Temp, i

   READ(*,*)  n                         ! read in the input array
   READ(*,*)  (a(i), i = 1, n)
   WRITE(*,*) "Input array:"            ! display the input array
   WRITE(*,*) (a(i), i = 1, n)

   Head = 1                             ! start with the beginning
   Tail = n                             ! start with the end
   DO                                   ! for each pair...
      IF (Head >= Tail)  EXIT           !    if Head crosses Tail, exit
      Temp    = a(Head)                 !    otherwise, swap them
      a(Head) = a(Tail)
      a(Tail) = Temp
      Head    = Head + 1                !    move forward
      Tail    = Tail - 1                !    move backward
   END DO                               ! loop back

   WRITE(*,*)                           ! display the result
   WRITE(*,*)  "Reversed array:"
   WRITE(*,*)  (a(i), i = 1, n)

END PROGRAM  Reverse
Click here to download this program.

Program Input and Output

If the input data consist of the following:
8
10  50  30  70  35  97  65  59
The out of the program is:
Input array:
10,  50,  30,  70,  35,  97,  65,  59

Reversed array:
59,  65,  97,  35,  70,  30,  50,  10

Discussion