You can use the assumed shape array A(1:, 1:)
in your function Alnorm
as follows:
program hw2
implicit none
interface
real*8 function Alnorm(A)
implicit none
real*8 A(1:, 1:), cur
integer i, j, M, N
end function Alnorm
end interface
real*8 Arr(3, 4), ans
integer i, j
read*, ((Arr(i,j), j=1,size(Arr,2)), i=1,size(Arr,1))
ans = Alnorm(Arr)
write(*, *) ans
pause
end program hw2
real*8 function Alnorm(A)
implicit none
real*8 A(1:, 1:), cur
integer i, j, M, N
Alnorm = 0.0
M = size(A, 1)
N = size(A, 2)
do i = 1, N
cur = 0.0
do j = 1, M
cur = cur + A(j,i)
enddo
if(Alnorm < cur) then
Alnorm = cur
endif
enddo
end function Alnorm
Please try to use explicit variable declaration by defining implicit none
.
It helps to avoid confusion especially for larger Fortran projects.
The size
function works well to get the rank for the matrix A
.
Don't forget to define the interface
block in your program for function
Alnorm
.
Considering the comments below your question and using constants M
and N
for the matrix, your program might look like:
program hw2
implicit none
integer, parameter :: M = 3, N = 4
real(kind=8) :: Arr(M, N), ans, Alnorm
integer :: i, j
read*, ((Arr(i,j), j=1,N), i=1,M)
ans = Alnorm(Arr, M, N)
write(*, *) ans
pause
end program hw2
real(kind=8) function Alnorm(A, M, N)
implicit none
integer, intent(in) :: M, N
real(kind=8), intent(in):: A(M, N)
real(kind=8) ::cur
integer :: i, j
Alnorm = 0.0
do i = 1, N
cur = 0.0
do j = 1, M
cur = cur + A(j,i)
enddo
if(Alnorm < cur) then
Alnorm = cur
endif
enddo
end function Alnorm
Tested with Intel Visual Fortran Compiler 19.1.0055.16, both programs write
back the maximal sum of a column defined in a M x N matrix.