回 帖 发 新 帖 刷新版面

主题:关于将指针数组用做可变大小数组的问题

module juzhen_chengfa
   implicit none
   contains
     function juzhen(a,b)
       real,pointer::a(:,:)
       real,pointer::b(:,:)
       real,pointer::c(:,:)
       integer::l,m,n
       integer::i,j
       print*,'input the number of juzhen:'
       read*,l,m,n
       allocate(a(1:l,1:m),b(1:m,1:n),c(1:l,1:n))
       print*,'请输入数组:'
       read*,a,b
       c=matmul(a,b)
       do i=1,l
         print*,c(i,:)
       end do
       deallocate(a,b,c)
    end function juzhen
end module juzhen_chengfa
program jieguo
   use juzhen_chengfa
   implicit none
end
上面是小菜鸟自己写的任意两个矩阵的乘法,请问各位高手,为什么不行。谢谢!

回复列表 (共2个回复)

沙发

搞这么复杂干吗?,你不懂的东西太多了,多看看书

program juzhen_chengfa
       real,allocatable::a(:,:),b(:,:),c(:,:)
       integer::l,m,n
       integer::i,j
       print*,'输入A矩阵的行、列'
       read(*,*) l,m
       print*,'输入B矩阵的行、列'
       read(*,*) m,n
       allocate(a(1:l,1:m),b(1:m,1:n),c(1:l,1:n))
       print*,'请输入A矩阵:'
       do i=1,l
          read(*,*) a(i,1:m)
       enddo
       print*,'请输入B矩阵:'
       do i=1,m
          read(*,*)  b(i,1:n)
       enddo
       write(*,*) ''
       c=matmul(a,b)
       do i=1,l
         print*,c(i,1:n)
       enddo
end 

测试结果:

 输入A矩阵的行、列
2 3
 输入B矩阵的行、列
3 4
 请输入A矩阵:
1 2 3
1 2 3
 请输入B矩阵:
4 5 6 7
4 5 6 7
4 5 6 7

   24.00000       30.00000       36.00000       42.00000
   24.00000       30.00000       36.00000       42.00000
Press any key to continue

板凳

楼主看看这样修改行吗?

module juzhen_chengfa
   implicit none
   contains
     subroutine juzhen()
       real,pointer::a(:,:)
       real,pointer::b(:,:)
       real,pointer::c(:,:)
       integer::l,m,n
       integer::i 
       l = 3; m = 4; n = 5
       allocate(a(1:l,1:m),b(1:m,1:n),c(1:l,1:n))
       !read(*, *) a,b
       call random_number(a)
       call random_number(b)
       c=matmul(a,b)
       do i=1,l
         print*,c(i,:)
       end do
       deallocate(a,b,c)
    end subroutine juzhen
end module juzhen_chengfa

program jieguo
   use juzhen_chengfa
   implicit none
   call juzhen()
end program

我来回复

您尚未登录,请登录后再回复。点此登录或注册