part1_solutions

part1_solutions - solutions.txt Mon Oct 31 08:24:34 2011 1...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: solutions.txt Mon Oct 31 08:24:34 2011 1 solutions.txt Mon Oct 31 08:24:34 2011 2 ******************************************* ./part1.f90 PROGRAM classproj1 !*** Program to multiply together two matrices mat1 & mat2 and store !*** the result in mat3. IMPLICIT NONE INTEGER, PARAMETER :: m=3,n=4,k=3 INTEGER :: matno,i,j,p INTEGER, DIMENSION(2) :: row=(/m,n/) REAL, DIMENSION(m,n) :: mat1 REAL, DIMENSION(n,k) :: mat2 REAL, DIMENSION(m,k) :: mat3,mat4 !**** !**** !**** !**** Input the two matrices from the keyboard Press return after each element matrix one then matrix two NOTE you could read each matrix is separately if you prefer! 68: PRINT*,mat2(i,:) 69: ENDDO 70: 71: PRINT*,"" 72: PRINT*,"Matrix1 * Matrix2 (My Answer)" 73: PRINT*,"" 74: 75: DO i=1,m 76: PRINT*,mat3(i,:) 77: ENDDO 78: 79: 80: PRINT*,"" 81: PRINT*,"Matrix1 * Matrix2 (Fortran Answer)" 82: PRINT*,"" 83: 84: DO i=1,m 85: PRINT*,mat4(i,:) 86: ENDDO 87: 88: 89: END PROGRAM classproj1 90: ******************************************* 91: DO matno=1,2 DO i=1,row(matno) PRINT*,"Enter matrix",matno," row",i IF (matno==1) THEN READ*,mat1(i,:) ELSE READ*,mat2(i,:) ENDIF ENDDO ENDDO !**** Perform the matrix multiplication !**** using three DO loops DO i=1,m !** rows in result matrix DO j=1,k !** cols in result matrix mat3(i,j)=0 DO p=1,n !** Along cols of mat1 and rows of mat2 mat3(i,j)=mat3(i,j)+mat1(i,p)*mat2(p,j) ENDDO ENDDO ENDDO !**** Perform the matrix multiplication !**** using Fortran's C=MATMUL(A,B) mat4=MATMUL(mat1,mat2) !**** Print out each matrix to the screen PRINT*,"Matrix One" PRINT*,"" DO i=1,m PRINT*,mat1(i,:) ENDDO 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: PRINT*,"" PRINT*,"Matrix Two" PRINT*,"" DO i=1,n ...
View Full Document

Ask a homework question - tutors are online