Computers in Engineering WWW Site - Example 7.3

# Example 7.3

#### FORTRAN Version

```!
PROGRAM P73
!
!
IMPLICIT NONE
REAL :: M1(3,4),M2(4,2),M3(3,2)
!
INTERFACE
SUBROUTINE MATSIN(M1,M2,I1,J1,I2,J2)
IMPLICIT NONE
REAL, INTENT(IN OUT) :: M1(:,:),M2(:,:)
INTEGER, INTENT(IN) :: J1,J2,I1,I2
END SUBROUTINE MATSIN
END INTERFACE
!
INTERFACE
SUBROUTINE MULT(M1,M2,M3,I1,J1,J2)
IMPLICIT NONE
REAL, INTENT(IN OUT) :: M1(:,:),M2(:,:),M3(:,:)
INTEGER, INTENT(IN) :: I1,J1,J2
END SUBROUTINE MULT
END INTERFACE
!
INTERFACE
SUBROUTINE PRNMAT(M3,IMAX,JMAX)
IMPLICIT NONE
REAL, INTENT(IN OUT) :: M3(:,:)
INTEGER, INTENT(IN) :: IMAX,JMAX
END SUBROUTINE PRNMAT
END INTERFACE
!
PRINT *,'This is Program P73 - Matrix multiplication'
PRINT *,'PROGRAM IS READING DATA INTO ARRAYS'
CALL MATSIN(M1,M2,3,4,4,2)
CALL PRNMAT(M1,3,4)
CALL PRNMAT(M2,4,2)
PRINT *,'NOW MULTIPLYING MATRICES'
CALL MULT(M1,M2,M3,3,4,2)
PRINT *,'SOLUTION:'
CALL PRNMAT(M3,3,2)
STOP
END PROGRAM P73
!
SUBROUTINE MATSIN(M1,M2,I1,J1,I2,J2)
!
IMPLICIT NONE
REAL, INTENT(IN OUT) :: M1(:,:),M2(:,:)
INTEGER, INTENT(IN) :: J1,J2,I1,I2
INTEGER :: I,J
!
!     ONE PER CARD
!
L1:   DO I=1,I1
END DO L1
!
!     ONE ROW/CARD
!
LOOP:   DO I=1,I2
END DO LOOP
RETURN
END SUBROUTINE MATSIN
!
!
!
SUBROUTINE MULT(M1,M2,M3,I1,J1,J2)
!
IMPLICIT NONE
REAL, INTENT(IN OUT) :: M1(:,:),M2(:,:),M3(:,:)
INTEGER, INTENT(IN) :: J1,J2,I1
INTEGER :: I,J,K
!
!      Now, we will multiply the two matrices
!
!
M3=0.0
DO K=1,J2
DO I=1,J1
DO J=1,I1
M3(J,K) = M3(J,K) + M1(J,I) * M2(I,K)
END DO
END DO
END DO
RETURN
END SUBROUTINE MULT
!
!
!
SUBROUTINE PRNMAT(M3,IMAX,JMAX)
!
IMPLICIT NONE
REAL, INTENT(IN OUT) :: M3(:,:)
INTEGER, INTENT(IN) :: IMAX,JMAX
INTEGER :: I,J
!
!
L3:   DO I=1,IMAX
PRINT *,(M3(I,J),J=1,JMAX)
END DO L3
PRINT *,' '
!
RETURN
END SUBROUTINE PRNMAT
```
DATA:
```45. 23. 63. 2.
54. 3. 64. 3.
75. 25. 45. 74.
45. 9.
2. 67.
4. 23.
54. 2.

```
OUTPUT:
```
+--------------------------------------------------+
|     32-bit Power for Lahey Computer Systems      |
|   Phar Lap's 386|DOS-Extender(tm) Version 7.0    |
|  Copyright (C) 1986-94 Phar Lap Software, Inc.   |
|           Available Memory = 14880 Kb            |
+--------------------------------------------------+

This is Program P73 - Matrix multiplication
PROGRAM IS READING DATA INTO ARRAYS
45.0000        23.0000        63.0000        2.00000
54.0000        3.00000        64.0000        3.00000
75.0000        25.0000        45.0000        74.0000

45.0000        9.00000
2.00000        67.0000
4.00000        23.0000
54.0000        2.00000

NOW MULTIPLYING MATRICES
SOLUTION:
2431.00        3399.00
2854.00        2165.00
7601.00        3533.00

```