1

I am currently working on a fortran matrix calculator. I have the program compiling and it seems to work except i am not receiving the correct results. When i select the addition feature my program accepts in two matrices and is to add them together. My result seems to be coming out wrong. Such as if i choose two matrices both of size 2x2 and list 1's as each entry i receive the addition result of 3,3,3. I cant figure out where i went wrong in the algorithm. I have not checked the other algorithms yet as I am trying to fix one algorithm at a time. Below is my code

  *START PROGRAM
  PROGRAM MAIN

  *DECLARATIONS
  INTEGER SELECTION, DONE, VALID, J, I, K, M, N
  INTEGER, DIMENSION(10,10):: SUM, A, B

  *INTITIALIZE
  M = 0
  N = 0
  J = 0
  I = 0
  K = 0
  DONE = 1
  VALID = 1

  *LOOP UNTIL USER CHOOSES TO QUIT

  DO WHILE(DONE .GT. 0)

  *DISPLAY MENU UNTIL VALID ENTRY IS ENTERED

  DO WHILE (VALID .GT. 0)
  *PRINT MENU
  PRINT *,'MATRIX CALCULATOR'
  PRINT *,'PLEASE MAKE A SELECTION'
  PRINT *,'1) MATRIX ADDITION'
  PRINT *,'2) MATRIX SUBTRACTION'
  PRINT *,'3) MATRIX MULTIPLICATION'
  PRINT *,'4) MATRIX TRANSPOSE'
  PRINT *,'5) QUIT PROGRAM'
  READ (*,*) SELECTION

  *VALID ENTRY CHECK
  IF (SELECTION .GT. 5 .OR. SELECTION .LT. 1) THEN
  PRINT *,'PLEASE ENTER A VALID SELECTION'
  ELSE 
  VALID= -1
  END IF

  *END WHEN VALID ENTRY IS ENTERED
  END DO

  *QUIT?
  IF (SELECTION .EQ. 5) THEN
  DONE = -1
  ELSE
  *OTHERWISE CONTINUE
  *GET DIMENTIONS INPUT 
  PRINT *,'PLEASE ENTER THE DIMENTIONS'
  PRINT *,'ENTER IN M:'
  READ (*,*) M
  PRINT *,'ENTER IN N:'
  READ (*,*) N
  *GET IN MATRIX
  PRINT *, 'PLEASE ENTER IN MATRIX A'
  DO J=1, M
  DO I=1, N
  PRINT *, 'ENTER IN VALUE I, I', J, I
  READ(*,*) A(M,N)
  END DO
  END DO

  PRINT *, 'PLEASE ENTER IN MATRIX B'
  DO J=1, M
  DO I=1, N
  PRINT *, 'ENTER IN VALUE I, I', J,I
  READ(*,*) B(M,N)
  END DO
  END DO

  *PERFORM DESIRED CALCULATION
  SELECT CASE(SELECTION)
  *CASE 1
  CASE (1)
   PRINT *,'MATRIX ADDITION'
   DO J=1,M
   DO I=1,N
   SUM(J,I)=A(J,I)+B(J,I)
   END DO   
   END DO

  *PRINT ADDITION RESULT
   PRINT *,'ADDITION RESULT'
   DO J=1, M
   DO I=1, N
   WRITE (*,*) SUM(J,I)
   END DO
   END DO
  *CASE 2
  CASE (2)
   PRINT *,'MATRIX SUBTRACTION'
   DO J=1,M
   DO I=1,N
   SUM(J,I)=A(J,I)-B(J,I)
   END DO   
   END DO

  *PRINT SUBTRACTION RESULT
   PRINT *,'SUBTRACTION RESULT'
   DO J=1, M
   DO I=1, N
   WRITE (*,*) SUM(J,I)
   END DO
   END DO
  *CASE 3
  CASE (3)
   PRINT *,'MATRIX MULTIPLICATION'
   DO J=1, M
   DO I=1, N
   DO K=1, N
   SUM(J,I) = SUM(J,I)+A(J,K)*B(I,J)
   END DO
   END DO
   END DO

  *PRINT MULTIPLICATION RESULT
   DO J=1, M
   DO I=1, N
   WRITE (*,*) SUM(J,I)
   END DO
   END DO
  *CASE 4
  CASE (4)
   PRINT *,'MATRIX TRANSPOSE'
   DO J=1, M
   DO I=1, N
   B(I,J) = A(J,I)
   END DO
   END DO
  *PRINT TRANSPOSE RESULT
   PRINT *, 'MATRIX A RESULT'
   DO J=1, M
   DO I=1, N
   WRITE (*,*) A(J,I)
   END DO
   END DO
   PRINT *, 'MATRIX B RESULT'
   DO J=1, M
   DO I=1, N
   WRITE (*,*) B(J,I)
   END DO
   END DO
  CASE DEFAULT
   PRINT *,'SOMETHING HAS GONE WRONG'
  END SELECT

  *USER SELECTED TO QUIT
  END IF
  END DO
  PRINT *, 'PROGRAM HAS ENDED'
  *END PROGRAM
  STOP
  END
1
  • I Have Changed the Write for the SUM(I,J) and also Increased the size of SUM, A, B. Still no luck Commented Mar 19, 2012 at 2:17

5 Answers 5

4

One way you to avoid some of the errors you made would be to use Fortran's array functions. You could, for example, sum arrays A and B, if they have the same size, with the single statement:

C = A + B

You could also use the intrinsic procedures matmul and transpose for matrix multiplication and transposition respectively.

Sign up to request clarification or add additional context in comments.

Comments

3

The main problem is that when reading the matrices, you are reading them to the wrong indices. instead of READ(*,*) A(M,N) and READ(*,*) B(M,N), you should have READ(*,*) A(J,I) and READ(*,*) B(J,I)

In addition to this, you need to have allocatable arrays instead of declaring sum, A and B as dimension(1,1). I also think naming an array SUM is bad practice as it is the same name as a Fortran intrinsic function, so I've called it C here. Declare them like this:

integer, allocatable, dimension(:,:) :: A, B, C

then after you read the dimensions from the user, do this:

allocate(A(m, n))

With these changes, I get (2,2;2,2) when adding (1,1;1,1) to (1,1;1,1).

In general, I would also suggest indenting your loops properly and not using all caps, it makes the code a lot harder to read.

Comments

1

This is the problem:

  *PRINT ADDITION RESULT
   PRINT *,'ADDITION RESULT'
   DO J=1, M
   DO I=1, N
   WRITE (*,*) SUM(N,M)
   END DO
   END DO

You should have WRITE (*,*) SUM(J,I).

2 Comments

Thank you. I corrected that issue however i am still getting an incorrect result. If i add 1,1;1,1 + 1,1;1,1 i receive an answer of 1;2;2;3
You've got exactly the same problem on your READ lines, too, i.e.: READ(*,*) A(M,N) -> READ(*,*) A(J,I). Similar for reading in B.
1

You declare SUM, A, and B to be 1x1 arrays, and then in the program you reference them outside of their declared bounds. Anything may happen.

Comments

1

Some of the mistakes noted in the other answers (e.g., out-of-bounds subscripting) can be automatically caught be compilers if you select the appropriate run time tests. Selecting extensive warning and error checking options of the compiler will help you find mistakes quicker. Which compiler are you using?

1 Comment

I am currently using a VAX compiler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.