I'm trying to write a subroutine to reverse the rows of an array. The following works, but explicitly declares the type of its input arr. Thus I'd need a separate subroutine to do the same thing for an array of type real. Surely there's a way to make it accept an array of arbitrary type - could someone help me with the syntax? Thanks!
SUBROUTINE flipud(arr)
integer, dimension(:,:), intent(inout) :: arr
integer, dimension(:,:), allocatable :: tmp
integer i, j, nrow, ncol, ierr
nrow = size(arr, 1)
ncol = size(arr, 2)
allocate(tmp(nrow, ncol), STAT=ierr)
tmp(:,:) = arr(nrow:1:-1, :)
arr = tmp
deallocate(tmp)
END SUBROUTINE flipud
Fortran does not have the equivalent of C++ templates. You can create an interface with module procedures, as sketched below. The code that is common to both subroutines could be placed in a file that is incorporated using the INCLUDE statement.
module foo
interface fliupud
module procedure flipud_i,flipud_r
end interface flipud
contains
subroutine flipud_i(arr)
integer, dimension(:,:), intent(inout) :: arr
! body
end subroutine flipud_i
!
subroutine flipud_r(arr)
real, dimension(:,:), intent(inout) :: arr
! body
end subroutine flipud_r
end module foo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.