# 5x5 Arrays - Sum & Difference - (Fortran)

Tags:
1. May 9, 2017

### Techno_Knight

1. The problem statement, all variables and given/known data

Create two 5x5 arrays, A & B, and ask the person to fill them out. Save those numbers in matrix_a.txt & matrix_b.txt respectively. Then, save the sum and difference of those numbers in sum.txt & diff.txt respectively.

Basically we need to create two arrays, fill them out, save the numbers in .txt files using the ofstream file_name("...") command, and then do the same (.txt) for their sum and difference.

2. Relevant equations

It needs to be done in C++ & Fortran languages.

3. The attempt at a solution

I have done the C++ version already:

Code (C):

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
double arrA[5][5];
double arrB[5][5];
double arrPAB[5][5];
double arrDAB[5][5];

int i, j, k, l;
double sumAB, difAB;

ofstream arrayA("matrix_a.txt");
ofstream arrayB("matrix_b.txt");
ofstream arrayPlus("sum.txt");
ofstream arrayMinus("dif.txt");

for(i = 0; i<5; i++)
{
for(j=0; j<5; j++)
{
cout << "Enter the values for Measurement No " << i << " in arrA(" << i << ") (" << j << "): ";
cin >> arrA[i][j];
arrayA << arrA[i][j] << " ";
}
}

cout << endl << endl;

for(k=0; k<5; k++)
{
for(l=0; l<5; l++)
{
cout << "Enter the values for Measurement No " << k << " in arrB(" << k << ") (" << l << "): ";
cin >> arrB[k][l];
arrayB << arrB[k][l] << " ";
}
}

sumAB = 0;
difAB = 0;

for(i=0, k=0; i<5, k<5; i++, k++)
{
for(j=0, l=0; j<5, l<5; j++, l++)
{
sumAB += (arrA[i][j] + arrB[k][l]);
difAB += (arrA[i][j] - arrB[k][l]);
}
}

arrayPlus << "The sum of the two Arrays is: " << sumAB << " ";
arrayMinus << "The difference between the two Arrays is: " << difAB << " ";

return 0;
}

I've checked it, and it works alright. But I have problems with the Fortran version:

Code (Fortran):

PROGRAM Arrays5x5

REAL, DIMENSION (5,5) :: ARRA
REAL, DIMENSION (5,5) :: ARRB
REAL, DIMENSION (5,5) :: ARRPAB
REAL, DIMENSION (5,5) :: ARRDAB

INTEGER :: I, J, K, L
REAL :: SUMAB, DIFAB

OPEN (UNIT=13, FILE='matrix_a.txt')
OPEN (UNIT=14, FILE='matrix_b.txt')
OPEN (UNIT=15, FILE='sum.txt')
OPEN (UNIT=16, FILE='dif.txt')

DO I = 0, 5
DO J = 0, 5
PRINT *, 'Enter the values in arrA(' ,I , ') (' ,J , '): '
WRITE(13,*) ARRA(I)(J), ' '
END DO
END DO
CLOSE(UNIT=13)

PRINT*
PRINT*

DO K = 0, 5
DO L = 0, 5
PRINT *, 'Enter the values in arrB(' ,K , ') (' ,L , '): '
WRITE(14,*) ARRA(K)(L), ' '
END DO
END DO
CLOSE(UNIT=14)

SUMAB = 0
DIFAB = 0

DO I = 0, K = 0, 5, 5
DO J = 0, L = 0, 5, 5
SUMAB = SUMAB + (ARRA(I)(J) + ARRB(K)(L))
DIFAB = DIFAB + (ARRA[I][J] - ARRB[K][L])
WRITE(15,*), 'The sum of the two Arrays is: ', SUMAB
WRITE(16,*), 'The diferrence between the two arrays is: ', DIFAB
END DO
END DO
CLOSE(UNIT=15)
CLOSE(UNIT=16)

END PROGRAM

Any help is appreciated!

2. May 9, 2017

### Staff: Mentor

Code (Fortran):
DO I = 0, 5
DO J = 0, 5
PRINT *, 'Enter the values in arrA(' ,I , ') (' ,J , '): '
WRITE(13,*) ARRA(I)(J), ' '
END DO
END DO
Do you realize that the body of you nested do loops is running 36 times, not 25 times?
Your do loops should be DO I = 1,5, and similar for the loop on J.

It looks like you have a typo here in the first two lines. For the sum or difference of two 2D arrays, use nested DO loops just like you used to enter values in each array (but run your loop indexes from 1 to 5, not 0 to 5).
Code (Fortran):
DO I = 0, K = 0, 5, 5
DO J = 0, L = 0, 5, 5
SUMAB = SUMAB + (ARRA(I)(J) + ARRB(K)(L))
DIFAB = DIFAB + (ARRA[I][J] - ARRB[K][L])
WRITE(15,*), 'The sum of the two Arrays is: ', SUMAB
WRITE(16,*), 'The diferrence between the two arrays is: ', DIFAB
END DO
END DO

What problems, specifically? Saying you have problems gives us just about zero to go on.

Does your Fortran program compile? If not, the compiler will give you information about where the syntax error is located.
If the program compiles, does it produce incorrect results? If so, what are the results, and how are they different from the results you expected.

3. May 9, 2017

### FactChecker

What problems are you having? Please don't make us guess.

4. May 9, 2017

### eys_physics

I believe it should be "ARRB" instead of "ARRA" on these line. Furthermore, it would be useful if you could post the error you are getting. Otherwise it is difficult help you.

5. May 9, 2017

### FactChecker

You are storing data in FORTRAN array index 0, which is not the default. Putting data outside of the legitimate array bounds can cause a lot of bazaar behavior. Start your FORTRAN loops at index 1.

6. May 9, 2017

### Techno_Knight

Yeah, that was a rookie mistake (well, all things considered I don't have tons of hands-on). I fixed that one now.

Yeah, sorry about that. I rushed it a bit, that's true (it was getting late and it's been a long day). Anyway, the problem is that it doesn't even compile. I looked a bit on the net and with this being pointed out:

I found out that I had written ARRB(K)(L) instead of the proper ARRB(K,L) (first time working with arrays with more than one dimension in Fortran).

Code (Fortran):

DO K = 1, 5
DO L = 1, 5
PRINT *, 'Enter the values in arrB(' ,K , ') (' ,L , '): '
WRITE(14,*) ARRB(K,L), ' '
END DO
END DO
CLOSE(UNIT=14)

With that fixed, I'm having trouble with the final nested loop, where I have to "create" the sum and difference of the two. The compiler gives me this:

---------- Capture Output ----------
> "C:\Emerald\gfortran.bat" -ffree-form BF.f BF
BF.f:40:12:

DO I = 1, K = 1, 5, 5
1
Error: Syntax error in DO statement at (1)
BF.f:41:12:

DO J = 1, L = 1, 5, 5
1
Error: Syntax error in DO statement at (1)
BF.f:46:3:

END DO
1
Error: Expecting END PROGRAM statement at (1)
BF.f:47:3:

END DO
1
Error: Expecting END PROGRAM statement at (1)

> Terminated with exit code 1.

So I guess the problem lies there.

7. May 9, 2017

### FactChecker

Yes. Those loops look wrong to me. As in the earlier loops, just control one index, I, in an outer loop and loop the other index, J within that outer loop. I don't think that you want the K and L indices at all. I would interpret the problem as asking for element-by-element sum and difference of the two matrices. That is a common thing to do with matrices. If you really want total sums of all elements, you can keep running totals as the user inputs each number. I think that your C version has the same misconception.

8. May 9, 2017

### eys_physics

I guess what you want here are two loops one over for I and another over K. However, I believe the syntax is not correct. You have to write with two Do statements.

9. May 9, 2017

### Techno_Knight

Well, the exercise is not exactly clear about this, but what I got from it is that I just need the sum of ARRA(1)(1) + ARRB(1)(1) + ... ARRA(5)(5) + ARRB(5)(5). I went with all 1s on ARRA & all 2s on ARRB for an easy "test drive", and the sum should be 75, while the difference should be -25. Problem is, I tried skipping K and L, but I don't get a good result:
Diferrence:
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
The diferrence between the two arrays is: 0.00000000
Sum:
The sum of the two Arrays is: 4.00000000
The sum of the two Arrays is: 6.00000000
The sum of the two Arrays is: 8.00000000
The sum of the two Arrays is: 10.0000000
The sum of the two Arrays is: 12.0000000
The sum of the two Arrays is: 14.0000000
The sum of the two Arrays is: 16.0000000
The sum of the two Arrays is: 18.0000000
The sum of the two Arrays is: 20.0000000
The sum of the two Arrays is: 22.0000000
The sum of the two Arrays is: 24.0000000
The sum of the two Arrays is: 26.0000000
The sum of the two Arrays is: 28.0000000
The sum of the two Arrays is: 30.0000000
The sum of the two Arrays is: 32.0000000
The sum of the two Arrays is: 34.0000000
The sum of the two Arrays is: 36.0000000
The sum of the two Arrays is: 38.0000000
The sum of the two Arrays is: 40.0000000
The sum of the two Arrays is: 42.0000000
The sum of the two Arrays is: 44.0000000
The sum of the two Arrays is: 46.0000000
The sum of the two Arrays is: 48.0000000
The sum of the two Arrays is: 50.0000000

The problem is that I have 4 variables. If I go with 4 Dos, well, the whole thing breaks. If I go with 2 loops, two of those are skipped, and I get faulty results.

10. May 9, 2017

### eys_physics

It is a bit unclear for me what is meant with sum and difference. For example sum can mean C=A+B, i.e. the elements of C can be C(i,j)=A(i,j)+B(i,j). Or the total sum of all elements in the matrices A and B, i.e. sum(C)=sum(A)+sum(B). As they ask you print the sum to file it seems to be first case. Then, you only need the two variables i and j. It will of course be similar for the difference.

11. May 9, 2017

### Techno_Knight

Well, it's a bit unclear to me as well to be honest. From what I'm getting, it is: Sum = [aij + bkl] & Difference = [aij - bkl]. So that's why I'm goign with the loops. Not to be a PITA, but would you mind turning what you're proposing I do into code, because I'm not sure I'm getting it.

12. May 9, 2017

### eys_physics

Well, I mean something like
Code (Fortran):

Do k=1, N
Do j=1, N
c(k,j)=a(k,j)+b(k,j)
end do
end do

But, it seems now like you should combine all elements of a and b, so $sum=[a_ij+b_kl]$. However, I still don't understand why you get problems if you use four loops. It should be possible.

13. May 9, 2017

### Techno_Knight

Ah yup, that works just fine. I guess I overcomplicated the C++ version and then tried to do the same with the Fortran one.

Either way, thanks a ton for the help everybody, I greatly appreciate it!

14. May 9, 2017

### eys_physics

You are welcome!