Register to reply 
FORTRAN 90 help matrixmuladd input from user defined file name 
Share this thread: 
#1
Oct2611, 03:31 PM

P: 3

Hi everyone
I am completely new to the Fortran. I have to make a program in fortran 90 to either add or multiply the matrix on user demand and ask user to enter the file name inorder to read the desired matrices. I came up with the the following code which does not read the program and produces runtime error before the open statement. And i am gonna make subroutines for each case(add and multiply) Can anyone help me identify the right way of making the source code in order to read the user defined input from input file and put the matrix on tothe screen as it is defined in the input file. Following the formatting to input matrix rowsa, colsa 2, 5, 6 8, 9, 7 Also fortran only does 10 * 10 matrix so how do i make sure it reads every entry of the program properly. ! 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 Do i= 1,rowsA Do j=1,colsA READ (11,*) A WRITE (*,*) "The first matrix is ",A End do 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 Do i= 1,rowsB Do j=1,colsB READ (30,*) B WRITE (*,*) "The second matrix is ",B End do End do close (30) END PROGRAM MAT_add_multiply Thanks very much. Muzzy 


#2
Oct2611, 05:13 PM

P: 875




#3
Oct2911, 08:25 PM

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 :) 


#4
Oct2911, 09:41 PM

P: 875

FORTRAN 90 help matrixmuladd 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.



#5
Oct3011, 12:45 AM

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 


#6
Oct3011, 08:25 AM

P: 875

The DOloops that you refer to take advantage of a Fortran feature called "implicit doloop"...this doloop does not need "do"/"enddo" enclosing statements; instead, it simply happens within a single set of parenthesis.
The construct "(B(i,j),j=1,colsB)" leaves i alone, but varies j from 1 to colsB. The line READ (30,*) (B(i,j),j=1,colsB) reads an entire row of matrix B; more precisely, it reads the ith row as the value for i remains constant but j is varied from 1 to colsB. The loop Do i= 1,rowsB READ (30,*) (B(i,j),j=1,colsB) End do would seem equivalent to a double loop like this: Do i= 1,rowsB Do j= 1,colsB READ (30,*) B(i,j) End do End do but will not work as desired due to the way you want to format your files. When reading sequential file like you are doing, the READ() command retrieves the next line in the file and reads from it as many values as requested; and so, the explicit doubledoloop above would only read the first value of every row and soon run out of file...for it to work, you would need to write your input file with one value per line, which looses it visual correspondance to a nice looking matrix. The WRITE statements have also been written to take advantage of the implicit doloop feature...because we do not know how many values per line we need to write, I have simply put a multiplier of 100 for the F7.2 specification...it will only use as many as needed. The multiplication and addition operations as shown are taking advantage of Fortran90 intrinsic features...just like the scalar addition c=a+b (c=3+7, for example) works in any other language, the array addition C=A+B works in Fortran90 (ala Matlab); the same goes for the intrinsic function 'matmul' (matrix multiplication)..it just does the doubleloop for you, no need to do anything else. The reason why I had to specify the dimensions of the matrices during addition and multiplication is because the matrices were declared larger than necessary since we did not know in advnace the necessary dimensions...it is possible to delay the creation of such matrices with "allocatable" but I did not want to introduce that at this time. Don't be shy about "asking" questions to the comipler...by that, I mean, experiment with simple programs that test one feature at a time, compile then, and see how it behaves. 


#7
Oct3011, 03:25 PM

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" 


#8
Oct3011, 03:30 PM

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. 


#9
Oct3011, 05:09 PM

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 


#10
Oct3011, 07:36 PM

P: 875

hhhmmm...my version of the program output the correct answer...



Register to reply 
Related Discussions  
Input .txt file into Fortran program  Programming & Computer Science  9  
FORTRAN Help: Concept of User Defined Functions  Engineering, Comp Sci, & Technology Homework  2  
Fortran Input File  Programming & Computer Science  1  
Using a batch file to create NAMELIST input file for FORTRAN  Programming & Computer Science  0  
Fortran; Calling an executable that in turn runs an input file  Programming & Computer Science  1 