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

by muzzy29
Tags: defined, file, fortran, input, matrixmuladd, user
 P: 875 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: 875 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
 P: 2 hey i tried the code when "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 " it count correctly in order when matrix 3,3 add together but i cant find the correct answer for the multiplication with this, same matrix 3,3 multiply together "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"
 P: 875 can you show me what matrices you are using and what results you are expecting and what results you are getting? did you figure how to enter input to the program? the first answer is either the character '+' or the character '*', right? Then, the file names of the matrices... Please list the files you are using, etc.
 P: 2 example: file 1 3,3 2,1,2 3,1,0 1,1,1 file 2 3,3 2,2,2 2,2,2 2,2,2 yes i can do the addition part the answer will be 4,3,4 5,3,2 3,3,3 but when the multiplication part it shows the answer is 4,2,4 6,2,0 2,2,2 which the answer should be 10,10,10 8,8,8 6,6,6
 P: 875 hhhmmm...my version of the program output the correct answer...

 Related Discussions Programming & Computer Science 9 Engineering, Comp Sci, & Technology Homework 2 Programming & Computer Science 1 Programming & Computer Science 0 Programming & Computer Science 1