Using an array coming from a module to be used in another subroutine or main program in Fortran -
i'd glad if me this. i'm studying modules in fortran, , have question. let's module creates matrix [a(3,3)] read user's input. then, i'd use such matrix in new subroutine can operation (for sake of simplicity let's sum). code looks this:
module matrixm contains subroutine matrixc integer i,j real, dimension(3,3) :: 10 i=1,3 20 j=1,3 read(*,*) a(i,j) 20 continue 10 continue end subroutine matrixc end module matrixm program matrix use matrixm real, dimension(3,3) :: b,c integer i,j call matrixc b=10.0 c=a+b write statements here... end
if input of is: 1 2 3 4 5 6 7 8 9 1 expect c[3,3] 11 12 13 14 15 16 17 18 19. however, result shows matrix c elemets of them equal 10.0. error have in program?, , more important, correct on what's use of module?. have similar issue on big problem i'm working on right now. thanks.
the problem have in program visible memory:
you read data in matrix a
local subroutine matrixc
. means, change not visible program.
the next thing is, variable a
in program implicitely defined real , result, doesn't throw error (keyword: implicit none).
there 2 easy solutions:
1: put definition of matrix a
in definition part of module:
module matrixm real, dimension(3,3) :: contains subroutine matrixc integer i,j i=1,3 j=1,3 read(*,*) a(i,j) end end end subroutine matrixc end module matrixm
2: use a
parameter subroutine , define in main program:
module matrixm contains subroutine matrixc(a) integer i,j real, dimension(3,3) :: i=1,3 j=1,3 read(*,*) a(i,j) end end end subroutine matrixc end module matrixm program matrix use matrixm implicit none real, dimension(3,3) :: a,b,c integer i,j call matrixc(a) b=10.0 c=a+b write statements here... end program
Comments
Post a Comment