matrix

Can someone check my code and see what is wrong :( it complies but the outputs are not correct. The numbers don't add up properly.

matrix.cpp
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
#include "matrix.h"
#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string>

using namespace std; 

Matrix::Matrix() 
{
	for(int i = 0; i < MAXROWS; ++i)
	{
		for(int l = 0; l < MAXCOLS; ++l)
		{
			elements[i][l] = 0;
		}
	}
}
void Matrix::operator++()
{
   	for(int i = 0; i < MAXROWS; ++i)
	{
		for(int l = 0; l < MAXCOLS; ++l)
		{
			elements[i][l] = ++elements[i][l];
		}
	}
}
void Matrix::operator+=(int array[3][3]) 
{
 	for(int i = 0; i < MAXROWS; ++i)
	{
		for(int l = 0; l < MAXCOLS; ++l)
		{
			elements[i][l] = array[i][l];
		}
	}
}
Matrix Matrix::operator-(Matrix& other) const
{ 
	for(int i = 0; i < MAXROWS; ++i)
	{
		for(int l = 0; l < MAXCOLS; ++l)
		{
			((const Matrix*)this)->Matrix::elements[i][l];
		}
	}
}
Matrix Matrix::operator+(Matrix& other) const
{
	for(int i = 0; i < MAXROWS; ++i)
	{
		for(int l = 0; l < MAXCOLS; ++l)
		{
			((const Matrix*)this)->Matrix::elements[i][l];
			
		}
	}
	
}
void Matrix::display()
{
	for(int i = 0; i < MAXROWS; ++i)
	{
		cout << '[';
		for(int l = 0; l < MAXCOLS; ++l)
		{
			cout << ' ' << elements[i][l] << ' ';
		}
		cout << ']' << endl;
	}
}


matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef __MATRIX_H__
#define __MATRIX_H__

#include <iostream>

using namespace std;

#define MAXROWS 3
#define MAXCOLS 3

class Matrix { 
    private:
        int elements[MAXROWS][MAXCOLS];
    public:
        Matrix();
        void operator++();
        void operator+=(int array[MAXROWS][MAXCOLS]);
        Matrix operator-(Matrix& other) const;
        Matrix operator+(Matrix& other) const;
        void display(); 
};

#endif 


main.cpp
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
#include <iostream>
#include "matrix.h"

using namespace std;

#define MAXROWS 3
#define MAXCOLS 3

int main()
{
    Matrix matrix1, matrix2, matrix3, matrix4;
    
    int array1[MAXROWS][MAXCOLS] = 
    {
      {1, 2, 3},
      {4, 5, 6}, 
      {7, 8, 9}
    };

    int array2[MAXROWS][MAXCOLS] = 
    {
      {10, 11, 12},
      {13, 14, 15}, 
      {16, 17, 18}
    };


    cout << "Matrix object 'matrix1' instantiated:" << endl;
    matrix1.display();
    
    cout << "2D array1 added to matrix1 using += " << endl;
    matrix1 += array1;
    matrix1.display();

    // Use overloaded assignment operator to copy the values across
    cout << "2D array2 added to matrix2 using += " << endl;
    matrix2 += array2;
    matrix2.display();

    cout << "All elements of matrix1 incremented with ++ " << endl;
    ++matrix1;
    matrix1.display();

    cout << "Using addition and subtraction operators" << endl;
    matrix3 = matrix1 + matrix2; 
    matrix4 = matrix2 - matrix1; 
    
    cout << "matrix3 = matrix1 + matrix2" << endl;
    matrix1.display();
    cout << "+" << endl;
    matrix2.display(); 
    cout << "--------------" << endl;
    matrix3.display(); 
    
    cout << "matrix4 = matrix2 - matrix1" << endl;
    matrix2.display();
    cout << "-" << endl;
    matrix1.display(); 
    cout << "--------------" << endl;
    matrix4.display(); 
    
    
    return 0;                
}


When i run it give me the output:
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
Matrix object 'matrix1' instantiated:
[ 0  0  0 ]
[ 0  0  0 ]
[ 0  0  0 ]
2D array1 added to matrix1 using += 
[ 1  2  3 ]
[ 4  5  6 ]
[ 7  8  9 ]
2D array2 added to matrix2 using += 
[ 10  11  12 ]
[ 13  14  15 ]
[ 16  17  18 ]
All elements of matrix1 incremented with ++ 
[ 2  3  4 ]
[ 5  6  7 ]
[ 8  9  10 ]
Using addition and subtraction operators
matrix3 = matrix1 + matrix2
[ 2  3  4 ]
[ 5  6  7 ]
[ 8  9  10 ]
+
[ 10  11  12 ]
[ 13  14  15 ]
[ 16  17  18 ]
[ 10449456  10449456  -1078913704 ]
[ 10454620  -1078913672  2380736 ]
[ 13  10444788  10449348 ]
matrix4 = matrix2 - matrix1
[ 10  11  12 ]
[ 13  14  15 ]
[ 16  17  18 ]
-
[ 2  3  4 ]
[ 5  6  7 ]
[ 8  9  10 ]
--------------
[ 10449456  10449456  -1078913704 ]
[ 10454620  -1078913672  2380736 ]
[ 13  10444788  10449348 ]


Line 35 in matrix.cpp: needs += (it's working by accident)
Lines 45 and 55 in matrix.cpp aren't doing anything

You need to step through the code with your mind - if you wrote the code, it shouldn't be difficult to do.
Otherwise, step through with a debugger (good to learn how to use one).
Last edited on
How would i fix line 45 and line 55? those are the lines that causing me problems. Other than that i believe the code is correct.
Well, to fix them, you could make them actually do something, aka what they are supposed to do. ;)

It seems they just need to add and subtract. It will require a temporary matrix to be created and take the value of *this, then subtract the values of the parameter.
Last edited on
How would i fix line 45 and line 55? those are the lines that causing me problems. Other than that i believe the code is correct.

If you believe that, try to run Lines 31-33 in main.cpp twice without fixing Line 35 in matrix.cpp and see if you get what you expect.

L B has given you sufficient clues, I think.

Your responses are leading me to believe that you are not understanding the code that you wrote.
Last edited on
There are also errors on Line 39 and 49 in matrix.cpp (read up on operator+ and look at your code VERY CAREFULLY).

operator+ and operator- also need to return something - again, read up on operators and compare your code with textbook examples.

...I really shouldn't be doing your debugging for you - I'm robbing you of your chance to practice - only trying to help because some of your bugs there are somewhat nasty.
Last edited on
OK i know this does not compile but i want to see if i'm on the right track:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Matrix Matrix::operator-(Matrix& other) const
{ 
	Matrix& other1 = *this;
	Matrix& elements(other1.MAXROWS,other1.MAXCOLS);
	
	for(int i = 0; i < other1.MAXROWS; ++i)
	{
		for(int l = 0; l < other1.MAXCOLS; ++l)
		{
			elements[i][l] = other1[MAXROWS][MAXCOLS] - other[MAXROWS][MAXCOLS];  
		}
	}
return elements; 
}

Suggestions:

1. Read more on operator-, in particular the return signature. Pay very close attention to simple examples.

2. You have bugs on Line 10 - you have two choices: think like the computer or use a debugger (when it compiles) and step through the program.

3. You have to think carefully about what Line 4 means.

Follow the suggestions above, try to fix your problems, and then come back and ask more questions.
Ok i finally got the code! thank you guys for the help :D i had to read to understand what my project and now that i did i was able to successfully create a temporary matrix and utilize it.
Just a note, macros are not members of anything. They are global across whole projects. see below
Last edited on
No, they're not. Macros are "local" to the translation unit they appear in. In other words, if a.h defines MACRO and b.cpp includes a.h but c.cpp doesn't, MACRO will be usable in a.h and in b.cpp after a.h has been included. c.cpp may define its own MACRO without colliding with the one declared on a.h.
Oh and the reason i didnt know much about macros is that we barely learned it today XD
Actually macros are a source of frustration for run-time errors back in the C-days. Nowadays with C++, we should try to use the newer features instead of using C macro. I have heard enough stories on how C macros ruin a program and waste developers hours and hours of debugging time.
Topic archived. No new replies allowed.