# FORTRAN 90 help- matrix-mul-add- input from user defined file name

by muzzy29
Tags: defined, file, fortran, input, matrixmuladd, user
 P: 838 program mat_add_multiply integer rowsA, colsA, rowsB, colsB, i, j real A(100,100) real B(100,100) real C(100,100) character operation, inputA*30, inputB*30 ! Ask user for the desired operation WRITE(*,*) "Enter the desired operation to perform as sum or multiply " Read (*,*) operation ! Ask user to open file for the first matrix WRITE(*,*) "" WRITE(*,*) "" WRITE(*,*) "Enter file name for the first matrix" Read (*,*) inputA OPEN (UNIT=20, file=inputA, STATUS='OLD') ! Read dimension of matrix A READ (20,*) rowsA,colsA ! Read each matrix(array) elements WRITE (*,*) "The first matrix is " Do i= 1,rowsA READ (20,*) (A(i,j),j=1,colsA) write(*,"(100F7.2,$)") (A(i,j),j=1,colsA) End do close (20) ! Ask user to open file for the Second matrix WRITE(*,*) "" WRITE(*,*) "" WRITE(*,*) "Enter file name for the second matrix" Read (*,*) inputB OPEN (UNIT=30, file=inputB, STATUS='OLD') ! Read dimension of matrix A READ (30,*) rowsB,colsB ! Read each matrix(array) elements WRITE (*,*) "The second matrix is " Do i= 1,rowsB READ (30,*) (B(i,j),j=1,colsB) write(*,"(100F7.2,$)") (B(i,j),j=1,colsB) End do close (30) WRITE(*,*) "" WRITE(*,*) "" WRITE(*,*) "The result is:" WRITE(*,*) "" If (operation == '*') then if (colsA.NE.rowsB) then write(*,*) "Sorry, A*B cannot be performed;" write(*,*) "Please check matrices dimensions" else C = matmul(A(1:rowsA,1:colsA),B(1:rowsB,1:colsB)) Do i= 1,rowsA WRITE(*,'(100F7.2)') (C(i,j),j=1,colsB) End do end if else if (operation == '+') then if (rowsA.NE.rowsB.or.colsA.NE.colsB) then write(*,*) "Sorry, A+B cannot be performed;" write(*,*) "Please check matrices dimensions" else C = A(1:rowsA,1:colsA)+B(1:rowsB,1:colsB) Do i= 1,rowsA WRITE(*,'(100F7.2)') (C(i,j),j=1,colsB) End do end if end if END PROGRAM MAT_add_multiply
 P: 3 THank you SO much it was really helpful :) Just had a issue with multiplication part that seems like it just multiplies 1st row and 2nd row of A with first column of B and writing out third column of A as third column of result (matrix C) and 2nd column of matrix B in the 2nd column of result (matrix C). by adjusting the dimensions. I am not completely aware of the loop that you coded up so I am not sure how would i fix that, if you can explain the formatting part while writing the result matrix and the looping part, I Will appreciate that. Thank you again for your generous help :)
P: 838

## FORTRAN 90 help- matrix-mul-add- input from user defined file name

please post again and include (quote) the precise lines you want me to explain...and I'll be happy to do so.
 P: 3 I basically dont understand the loops which i included below.I do not understand that how fortran reads these loops and i want to write flowchart so if you can tell me how these loops works and the formatting statement that you used in write statement. Also there is some issue with 2nd loop (multiplication loop) and what is matmul? did not I need to make matrix C entries 0 before I add a*b entries in matrix C ? "Do i= 1,rowsB READ (30,*) (B(i,j),j=1,colsB) write(*,"(100F7.2,\$)") (B(i,j),j=1,colsB) End do" "C = matmul(A(1:rowsA,1:colsA),B(1:rowsB,1:colsB)) Do i= 1,rowsA WRITE(*,'(100F7.2)') (C(i,j),j=1,colsB) End do" "C = A(1:rowsA,1:colsA)+B(1:rowsB,1:colsB) Do i= 1,rowsA WRITE(*,'(100F7.2)') (C(i,j),j=1,colsB) End do " Thank you