public
Created

Daily programmer #63 easy: Reverse an Array

  • Download Gist
daily63e.f03
FORTRAN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
! File: daily63e.f08
! Programmer: Cameron Carroll
! Created June 2012
! Last Modified June 13, 2012
 
! Purpose: Implement a function that takes an integer and an array,
! and reverses the first (integer) items of that array.
!
! For Reddit DailyProgrammer 63 Easy
! http://www.reddit.com/r/dailyprogrammer/comments/uw14f/6112012_challenge_63_easy/
 
 
 
module Daily63e
implicit none
 
contains
function reverseN(n, array, numel)
integer, intent(in) :: numel, array(numel), n
integer :: itemCounterUp, itemCounterDown, activePortion(n), &
& reverseN(numel), remainder(numel - n)
 
itemCounterDown = n
itemCounterUp = 1
 
do while (itemCounterDown /= 0)
activePortion(itemCounterUp) = array(itemCounterDown)
itemCounterDown = itemCounterDown - 1
itemCounterUp = itemCounterUp + 1
end do
 
remainder = array(n+1:numel)
reverseN(1:size(activePortion)) = activePortion
reverseN(size(activePortion)+1:size(activePortion)+size(remainder)) = remainder
 
end function reverseN
 
 
end module Daily63e
 
!-------------------------------------------------------------------!
! Driver !
!-------------------------------------------------------------------!
program main
 
use Daily63e
implicit none
 
integer :: ary(5), newary(5)
integer :: ary2(10), newary2(10)
 
ary = [1, 2, 3, 4, 5]
newary = reverseN(3, ary, size(ary))
print*, newary
 
ary2 = [950, 485, 2734, 5749, 54473, 49430, 4837, 439430, 438438, 2710]
newary2 = reverseN(8, ary2, size(ary2))
print*, newary2
 
 
 
end program main

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.