假设Shape数组需要Fortran中的显式接口

时间:2018-04-04 19:55:13

标签: fortran fortran90

林尝试写为一个简单的程序,首先要求数量为n的代码,然后创建具有在它下它与3在它的主对角线3S,1和0以及一个矢量(n)的n×n的矩阵不平衡的位置和2在偶数位置。然后它必须包含一个子程序,它将两者都相乘而不使用matmul()

program P13

implicit none

integer(4) :: n, i, j
integer, dimension(:), allocatable:: v 
integer, dimension(:,:), allocatable :: m
integer, dimension(:), allocatable :: r


write(*,*) "Insert n"
read(*,*) n



allocate (v(1:n))
allocate (m(1:n,1:n))

v(1:n:2) = 3
v(2:n:2) = 2
m = 0

DO i=1,n,1

   m (i,i:n)=1

END DO

Do i=1,n,1
 m (i,i)=3
 End do 

call matrmul(n, m, v)
end program

subroutine matrmul(n, b, o, t)
implicit none

integer(4), intent(in) :: n
integer(4) :: i, j
integer, dimension(:), intent(in) :: b 
integer, dimension(:,:),intent(in) :: o
integer, dimension(:), intent(out) :: t

DO i=1,n,1

t(i) = sum(b*o(:,i))
END DO

write(*,'(I2)') t

end subroutine 

我收到错误消息'matrmul'所需的显式接口(1):假定形状参数

我该如何解决这个问题?感谢

1 个答案:

答案 0 :(得分:1)

堆栈溢出中有很多示例,它们将向您展示如何创建显式接口。但是,由于您为主程序中的所有数组分配内存并将大小传递给子例程,只需使用n在子例程中声明所有数组。

subroutine matrmul(n, b, o, t)
  implicit none

  integer(4), intent(in) :: n
  integer(4) :: i, j
  integer, dimension(n), intent(in) :: b 
  integer, dimension(n,n),intent(in) :: o
  integer, dimension(n), intent(out) :: t