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


by muzzy29
Tags: defined, file, fortran, input, matrixmuladd, user
muzzy29
muzzy29 is offline
#1
Oct26-11, 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
Phys.Org News Partner Engineering news on Phys.org
Lifting the brakes on fuel efficiency
PsiKick's batteryless sensors poised for coming 'Internet of things'
Researcher launches successful tech start-up to help the blind
gsal
gsal is offline
#2
Oct26-11, 05:13 PM
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
muzzy29
muzzy29 is offline
#3
Oct29-11, 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 :)

gsal
gsal is offline
#4
Oct29-11, 09:41 PM
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.
muzzy29
muzzy29 is offline
#5
Oct30-11, 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
gsal
gsal is offline
#6
Oct30-11, 08:25 AM
P: 838
The DO-loops that you refer to take advantage of a Fortran feature called "implicit do-loop"...this do-loop does not need "do"/"end-do" 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 i-th 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 double-do-loop 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 do-loop 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 Fortran-90 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 Fortran-90 (a-la Matlab); the same goes for the intrinsic function 'matmul' (matrix multiplication)..it just does the double-loop 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.
YunzhE
YunzhE is offline
#7
Oct30-11, 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"
gsal
gsal is offline
#8
Oct30-11, 03:30 PM
P: 838
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.
YunzhE
YunzhE is offline
#9
Oct30-11, 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
gsal
gsal is offline
#10
Oct30-11, 07:36 PM
P: 838
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