[Fortran] 纯文本查看 复制代码
Program matrix
implicit none
integer n
parameter (n=16) ! remember setup dimension first!!!
real:: a(n,n), b(n,n), c(n,n)
open(11,status='unknown',file='c.DAT')
read(11,*) c
open(12,status='unknown',file='b.DAT')
read(12,*) b
call matmul(a,b,n,n,c,n,n)
write(*,*) 'Your new matrix looks like: '
write(*,*)
call output (a,n)
write(*,*)
stop
end
subroutine output (a,n)
implicit none
integer n
real a(n,n)
integer i,j
character(len = 100) :: for = '(??(1X, F8.1))'
write (for(2:3), '(i2)') n
do i=1,n
write (*, fmt=for) (a(i,j), j=1,n)
open(21,status='unknown',file='a.dat') ! your result is here
write(21,fmt=for) (a(i,j),j=1,n)
enddo
return
end subroutine output
subroutine matmul (a, b, br, bc, c, cr, cc)
implicit none
integer br
integer bc
real b(br, bc)
integer cr
integer cc
real c(cr,cc)
real a(br,cc)
integer i, j, k
if (bc .ne. cr) then
write (*,*) 'size error :('
stop
endif
do i=1, br
do j=1, cc
a(i,j)=0
do k=1, bc
a(i,j) = a(i,j) +b(j,k) * c(k,i)
enddo
enddo
enddo
return
end