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

In summary: READ statement and any variable following the * in a READ statement must be of the same type, so READ (30,*) (B(i,j),j=1,colsB) is much more convenient.I am not sure what you mean by "adjusting the dimensions".The statement C = matmul(A(1:rowsA,1:colsA),B(1:rowsB,1:colsB))takes all elements of rowsA and colsB of A and B respectively and calculates their matrix product...of course, this doesn't make sense if rowsA doesn't equal colsB (that is, if the number of columns in A is not the same as the
  • #1
muzzy29
3
0
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 going to 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
 
Engineering news on Phys.org
  • #2
Code:
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
 
  • #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
please post again and include (quote) the precise lines you want me to explain...and I'll be happy to do so.
 
  • #5
I basically don't 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
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.
 
  • #7
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 can't 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
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
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
hhhmmm...my version of the program output the correct answer...
 

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

1. What is FORTRAN 90 and how is it different from other programming languages?

FORTRAN 90 is a programming language primarily used for scientific and mathematical computing. It is an updated version of the original FORTRAN language and includes new features such as support for modules, dynamic memory allocation, and the ability to pass arrays as arguments. It is specifically designed for numerical calculations and is known for its speed and efficiency.

2. How do I perform matrix multiplication and addition in FORTRAN 90?

To perform matrix multiplication and addition in FORTRAN 90, you can use the built-in functions MATMUL and MATADD respectively. These functions take two arrays as arguments and return the result of the operation. Make sure the arrays are properly dimensioned before using these functions.

3. Can I input data from a user-defined file name in FORTRAN 90?

Yes, you can input data from a user-defined file name in FORTRAN 90 by using the OPEN statement. This statement allows you to open a file and assign it to a logical unit, which can then be used to read data from the file. You can also specify the file name and access mode (read, write, or append) in the OPEN statement.

4. How do I declare and use arrays in FORTRAN 90?

Arrays in FORTRAN 90 are declared using the DIMENSION keyword, followed by the array name and its dimensions. For example, to declare a 2D array named "matrix" with dimensions 3x3, you would use the statement DIMENSION matrix(3,3). To access elements in the array, you can use array indexing, where the first index represents the row and the second index represents the column.

5. How can I get help with specific FORTRAN 90 commands and syntax?

There are various resources available for getting help with FORTRAN 90 commands and syntax. You can refer to the official FORTRAN 90 language specification, online tutorials and guides, or seek help from experienced FORTRAN programmers. Additionally, many programming forums and communities have dedicated sections for FORTRAN where you can ask for help and receive guidance from other users.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
4
Views
681
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top