5x5 Arrays - Sum & Difference - (Fortran)

Click For Summary

Discussion Overview

The discussion revolves around a homework assignment involving the creation and manipulation of two 5x5 arrays in Fortran and C++. Participants are tasked with filling the arrays, saving them to text files, and calculating their sum and difference.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a working C++ implementation for creating and manipulating the arrays.
  • Another participant points out that the Fortran code has issues with loop indexing, suggesting that the loops should start from 1 instead of 0.
  • Concerns are raised about the Fortran code running more iterations than intended due to incorrect loop bounds.
  • Participants identify specific syntax errors in the Fortran code, including incorrect array indexing and DO loop structures.
  • One participant suggests that the final nested loop for calculating the sum and difference should not use two separate indices for the arrays, advocating for a more straightforward element-wise approach.
  • Another participant expresses frustration over the lack of clarity regarding the specific problems faced with the Fortran code, emphasizing the need for detailed error reporting.
  • Corrections are made regarding the use of array indices in Fortran, with one participant noting the transition from using parentheses to commas for multi-dimensional arrays.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct the loop indexing in the Fortran code and clarify the intended operations on the arrays. However, there is no consensus on the best approach to implement the sum and difference calculations, with differing opinions on whether to use nested loops or maintain running totals.

Contextual Notes

Participants note that the Fortran program does not compile due to syntax errors, and there are unresolved issues regarding the handling of array indices and loop structures. The discussion highlights the importance of understanding array manipulation in Fortran, especially for those new to the language.

Const@ntine
Messages
285
Reaction score
18

Homework Statement



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.

Homework Equations



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

The Attempt at a Solution



I have done the C++ version already:

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++)
    {
   count << "Enter the values for Measurement No " << i << " in arrA(" << i << ") (" << j << "): ";
   cin >> arrA[i][j];
   arrayA << arrA[i][j] << " ";
    }
  }

  count << endl << endl;

 for(k=0; k<5; k++)
  {
   for(l=0; l<5; l++)
    {
   count << "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:

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 , '): '
READ *, 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 , '): '
READ *, ARRA(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!
 
Physics news on Phys.org
Fortran:
DO I = 0, 5
DO J = 0, 5
PRINT *, 'Enter the values in arrA(' ,I , ') (' ,J , '): '
READ *, 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).
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

Darthkostis said:
But I have problems with the Fortran version:
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.
 
  • Like
Likes   Reactions: FactChecker
What problems are you having? Please don't make us guess.
 
  • Like
Likes   Reactions: mfb
Darthkostis said:
DO K = 0, 5
DO L = 0, 5
PRINT *, 'Enter the values in arrB(' ,K , ') (' ,L , '): '
READ *, ARRA(K)(L)
WRITE(14,*) ARRA(K)(L), ' '
END DOi
END DO

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.
 
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.
 
Mark44 said:
Fortran:
DO I = 0, 5
DO J = 0, 5
PRINT *, 'Enter the values in arrA(' ,I , ') (' ,J , '): '
READ *, 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.

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

Mark44 said:
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.

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

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:

eys_physics said:
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.

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

Fortran:
DO K = 1, 5
DO L = 1, 5
PRINT *, 'Enter the values in arrB(' ,K , ') (' ,L , '): '
READ *, 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.
 
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.
 
  • Like
Likes   Reactions: Const@ntine
Darthkostis said:
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).

Fortran:
DO K = 1, 5
DO L = 1, 5
PRINT *, 'Enter the values in arrB(' ,K , ') (' ,L , '): '
READ *, 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.
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.
 
FactChecker said:
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.

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

eys_physics said:
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.
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
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
eys_physics said:
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.

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
Well, I mean something like
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
eys_physics said:
Well, I mean something like
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.
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!
 
  • Like
Likes   Reactions: FactChecker
  • #14
You are welcome!
 
  • Like
Likes   Reactions: Const@ntine

Similar threads

Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K