- #1
Parzeevahl
- 6
- 0
- Homework Statement
- Multiply two 2x2 matrices of your choice in Fortran and check that the code gives the correct result.
- Relevant Equations
- arrays and do loops
I have tried to do this using arrays and do loops:
So, the code reads the matrices, element-by-element, from the user, and uses the matrix multiplication formula to give the results. But it is giving wrong results. I tried multiplying 2 unit matrices as a check, and the result is not a unit matrix. What went wrong?
Fortran:
program matrixmul
implicit none
real A(2, 2), B (2, 2), C (2, 2)
integer i, j, k
write (*, *) 'Input: First matrix'
do i = 1, 2
do j = 1, 2
read (*, *) A (i, j)
enddo
enddo
write (*, *) 'Input: Second matrix'
do i = 1, 2
do j = 1, 2
read (*, *) B (i, j)
enddo
enddo
do i = 1, 2
do j = 1, 2
do k = 1, 2
C (i, j) = C (i, j) + A (i, k) * B (k, j)
enddo
enddo
enddo
write (*, *) C
end
So, the code reads the matrices, element-by-element, from the user, and uses the matrix multiplication formula to give the results. But it is giving wrong results. I tried multiplying 2 unit matrices as a check, and the result is not a unit matrix. What went wrong?