Create a gist now

Instantly share code, notes, and snippets.

Daily programmer #63 easy: Reverse an Array
! 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment