- #1

- 285

- 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++)
{
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;
}
```

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
```