Using sequences for a matrix to add and subtract

I'm trying to use a set of sequences to add and subtract a matrix that is loaded into the program from a txt file, but I've got a mistake at the beginning of the code in the int main () section that I can't figure out. I'm not sure on how to reference to the separate add and subtract sequences like I have for the fill and print sequences. I've included notes within the code. This is for a school project and I have to use the sequence set up. I think that I am close, but unsure of where to fix the problem. Any help or advice would be greatly appreciated.

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;
        }
    }
}
what is M and N? I suspect they are both 3, given that the matrices are 3x3. In that case, use 3 and lose the extra parameters.

add takes 3 matrices, but you only provided 2.
add(mat1, mat2, mat3, 3, 3);
but if you do the mxn fix, its just add (mat1, mat2, mat3);

cout / print and such do not belong in add and subtract etc. add and subtract should... add and subtract. print should print. Mixing your interface and your work is trouble in the long run: what if tomorrow you had to do this work in a GUI program that did not use cout but edit1.settext(..) type calls? You have to rewrite add and subtract, why? If you let print functions handle all the output, then you only have to rewrite those, all the rest still works, do you see it?

later you will get the tools to say c = a+b for your matrices. These first initial programs... where you lack tools .... are painful.
Last edited on
Perhaps consider something like this:

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
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

constexpr size_t ArrSze {3};

using Array = double[ArrSze][ArrSze];

void Fill(ifstream&, Array);
void Print(const Array);
void Add(const Array, const Array, Array);
void Subtract(const Array, const Array, Array);

int main() {
	Array A{}, B{}, mat1{}, mat2{};
	ifstream infile("in.txt");

	if (!infile)
		return (std::cout << "Cannot open file\n"), 1;

	Fill(infile, A);
	Fill(infile, B);

	cout << "Matrix A:\n";
	Print(A);
	cout << '\n';

	cout << "Matrix B:\n";
	Print(B);
	cout << '\n';

	cout << "Matrix A + B:\n";
	Add(A, B, mat1);
	cout << "A + B\n";
	Print(mat1);
	cout << '\n';

	cout << "Matrix A - B:\n";
	Subtract(A, B, mat2);
	cout << "A - B\n";
	Print(mat2);
	cout << '\n';
}

void Fill(ifstream& infile, Array mat)
{
	for (size_t i = 0; i < ArrSze; ++i)
		for (size_t j = 0; j < ArrSze; ++j)
			infile >> mat[i][j];
}

void Print(const Array mat)
{
	for (size_t i = 0; i < ArrSze; ++i) {
		for (size_t j = 0; j < ArrSze; ++j)
			cout << mat[i][j] << "     ";

		cout << '\n';
	}
}

void Add(const Array mat1, const Array mat2, Array mat3)
{
	for (size_t i = 0; i < ArrSze; ++i)
		for (size_t j = 0; j < ArrSze; ++j)
			mat3[i][j] = mat1[i][j] + mat2[i][j];
}

void Subtract(const Array mat1, const Array mat2, Array mat3)
{
	for (size_t i = 0; i < ArrSze; ++i)
		for (size_t j = 0; j < ArrSze; j++)
			mat3[i][j] = mat1[i][j] - mat2[i][j];
}

Topic archived. No new replies allowed.