1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
void Fill(double[3][3], int, int);
void Print(double[3][3], int, int);
void Add(double[3][3], int, int);
void Subtract(double[3][3], int, int);
using namespace std;
ifstream infile;
int main()
{
double A[3][3], B[3][3], mat1, mat2, mat3;
infile.open("in.txt");
//loads data from in.txt for matrix a and b
Fill(A, 3, 3);
Fill(B, 3, 3);
cout << "Matrix A: " << endl;
Print(A, 3, 3); //shows matrix a
cout << endl;
cout << "Matrix B: " << endl;
Print(B, 3, 3); //shows matrix b
cout << endl;
//for lines 36 to 49 are my issue
//issue with this section of code, trying to load the code from in.txt
//to add and subtract matrix a and b
//add matrix a to matrix b
Fill(A, 3, 3);
Fill(B, 3, 3);
cout << "Matrix A+B: " << endl;
Add(mat1,mat2,3,3);
cout <<"A +B" << endl;
Print(mat3, 3, 3);
cout << endl;
//subtract matrix b from a
Fill(A, 3, 3);
Fill(B, 3, 3);
cout << "Matrix A-B: " << endl;
//Print();
cout << endl;
infile.close();
}
//loads matrix a and b from in.txt
void Fill(double mat[3][3], int m, int n)
{
int i, j;
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
infile >> mat[i][j];
}
}
}
//prints matrix a and b from in.txt
void Print(double mat[3][3], int m, int n)
{
int i, j;
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}
//adds matrix a and b
void Add(double mat1[3][3], double mat2[3][3], double mat3[3][3], int m, int n)
{
int i, j;
for(i=0; i<m; i++)
{
for(j=0; j<n;j++)
{
mat3[i][j]= mat1[i][j]+mat2[i][j];
cout << "Matrix A + Matrix B equals: " << mat3[i][j] << endl;
Print(mat3,3,3);
}
}
}
//subtracts matrix b from a
void Subtract(double mat1[3][3], double mat2[3][3], double mat3[3][3], int m, int n)
{
int i, j;
for(i=0; i<m; i++)
{
for(j=0; j<n;j++)
{
mat3[i][j]= mat1[i][j]-mat2[i][j];
cout << "Matrix A - Matrix B equals: " << mat3[i][j] << endl;
}
}
}
|