Fortran Algorithm Help - Getting wrong results?

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.

Up vote 1 down vote favorite share g+ share fb share tw.

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 algorithm matrix fortran fortran90 matrix-multiplication link|improve this question edited Mar 19 at 2:16 asked Mar 19 at 1:33dtturner1284 50% accept rate.

I Have Changed the Write for the SUM(I,J) and also Increased the size of SUM, A, B. Still no luck – dtturner12 Mar 19 at 2:17.

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.

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.

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

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 – dtturner12 Mar 19 at 1:46 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. – VolatileStorm Mar 19 at 7:37.

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.

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?

I am currently using a VAX compiler – dtturner12 Mar 19 at 15:16.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions