Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
PROGRAM matmult
implicit none
integer, dimension (:,:), allocatable :: array1
integer, dimension (:,:), allocatable :: array2
integer, dimension (:,:), allocatable :: array3
integer :: L, M, N, i, j, k, k2, asum
character (len = 15) :: l2, m2, n2
! Prompt for 3 integers (call them L, M, and N)
! They represent the dimensions of the arrays
! The first array will be dimension L x M, the second will be M x N
! and the product array, L x N
CALL GET_COMMAND_ARGUMENT(1, l2)
CALL GET_COMMAND_ARGUMENT(2, m2)
CALL GET_COMMAND_ARGUMENT(3, n2)
read(l2,'(i4)') L
read(m2,'(i4)') M
read(n2,'(i4)') N
allocate (array1(L, M))
allocate (array2(M, N))
allocate (array3(L, N))
! Initialize the first array such that array(i,j) = 10i+j
do i = 1, L
do j = 1, M
array1(i,j) = (10*i)+j
end do
end do
! Initialize the second array such that array(i,j) = 20i+2j
do i = 1, M
do j = 1, N
array2(i,j) = (20*i)+(2*j)
end do
end do
! Multiply arrays
! Matrix multiplication C = AB
! C(i,j) = the sum product of row i of A and column j of B
do i = 1, L
do j = 1, N
do k = 1, M
asum = asum + (array1(i, k) * array2(k, j))
end do
array3(i,j) = asum
write (*,*) asum
end do
end do
! Print the array row-by-row to STDOUT (terminal)
! To print elements of an array in a row, you can define a loop as part of the WRITE
! Assuming that vec is a one-dimensional array of length 5 and j is a declared variable,
! the following will print that vector as a row
! WRITE(*,*) (vec(j), j=1, 5)
! L x N
do i = 1, N
WRITE(*,*) (array3(j,i), j=1, L)
end do
! Deallocate arrays
! If you allocate it, you need to deallocate it
deallocate (array1)
deallocate (array2)
deallocate (array3)
END PROGRAM matmult
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more