Matrix mult help (pointers)

Hi guys,

Having an issue with matrix multiplication. For the final matrix, I get the proper size for example A[d][e] * B[x][y]= C[d][y].

I also know the algorithm is correct (I found similar ones online) but the damn program keeps crashing, and I have been at it for some time.

Everything else in the program works fine, for example addition, subtraction, copy constructor etc.

Thanks,

Mike

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

class Matrix{
public:
	Matrix(){}
	Matrix(int a, int b):row(a),col(b){
		int i;
		pointer=new int*[row];
		for(i=0;i<row;i++)
			pointer[i]=new int[col];
	}
	Matrix(Matrix& m){
		row=m.row;
		col=m.col;
		int i,j;
		pointer=new int*[row];
		for(i=0;i<row;i++)
			pointer[i]=new int[col];

		for(i=0;i<row;i++)
			for(j=0;j<col;j++)
				pointer[i][j]=m.pointer[i][j];
	}
	Matrix operator=(Matrix& m){
		row=m.row;
		col=m.col;
		int i, j;
		pointer=new int*[row];
		for(i=0;i<row;i++)
			pointer[i]=new int[col];
		for(i=0;i<row;i++)
			for(j=0;j<col;j++)
				pointer[i][j]=m.pointer[i][j];
		return *this;
	}
	Matrix operator*(Matrix& m){
		Matrix solution;
		solution.row=row;
		solution.col=m.col;
	
		cout<<"-------------------------\n";
		cout<<"M1\n";
		cout<<"row: "<<row<<endl;
		cout<<"col: "<<col<<endl;
		cout<<"+++++++++++++++++++++++++\n";

		cout<<"M2\n";
		cout<<"row: "<<m.row<<endl;
		cout<<"col: "<<m.col<<endl;

		cout<<"+++++++++++++++++++++++++\n";
		cout<<"Final row: "<<solution.row<<endl;//good
		cout<<"Final col: "<<solution.col<<endl;//good

		int i,j,k;
		solution.pointer=new int*[solution.row]();//initialized to zero
		for(i=0;i<solution.row;i++)
			solution.pointer[i]=new int[solution.col]();//initialized to zero

		


		for(i=0;i<row;i++){
			for(j=0;j<m.col;j++){
				for(k=0;k<col;k++){
					solution.pointer[i][j]+=m.pointer[i][k]*pointer[k][j];////PROBLME HERE ON THIS LINE!!!!!!!!!!!!!!!!
				}
			}
		}
			
		
		return solution;
	}
	Matrix operator-(Matrix& m){
		Matrix solution;
		solution.row=row;
		solution.col=col;
		
		int i,j;
		solution.pointer=new int*[solution.row];
		for(i=0;i<solution.row;i++)
			solution.pointer[i]=new int[solution.col];
		for(i=0;i<solution.row;i++)
			for(j=0;j<solution.col;j++)
				solution.pointer[i][j]=m.pointer[i][j]-pointer[i][j];
		return solution;
	}
	bool check_legality(Matrix& m){
		if(m.row==row && m.col==col)return true;
		else
			return false;
	}
	Matrix operator+(Matrix& m){
		Matrix solution;
		int i, j;
		solution.row=m.row;
		solution.col=m.col;
		solution.pointer=new int*[solution.row];
		for(i=0;i<solution.row;i++)
			solution.pointer[i]=new int[solution.col];
		for(i=0;i<solution.row;i++)
			for(j=0;j<solution.col;j++)
				solution.pointer[i][j]=m.pointer[i][j]+pointer[i][j];
		return solution;
	}
	~Matrix(){
		for(int i=0;i<row;i++)
			delete[]pointer[i];
		delete[] pointer;
	}   
	friend istream& operator>>(istream& in, Matrix& m);
	friend ostream& operator<<(ostream& out, Matrix& m);
private:
	int **pointer;
	int row, col;
}; 


int main(){

	
	Matrix m1(4,2), m2(2,4);

	cin>>m1;
	cout<<endl<<m1<<endl;

	cin>>m2;
	cout<<endl<<m2<<endl;
	
	Matrix mult=m2*m1;

	cout<<mult;
	
	_getch();
	return 0;
}	
ostream& operator<<(ostream& out, Matrix& m){
	for(int i=0;i<m.row;i++){
		for(int j=0;j<m.col;j++){
			out<<m.pointer[i][j]<<" ";
		}
		out<<endl;
	}
	cout<<endl;
	return out;
}
istream& operator>>(istream& in, Matrix& m){
	for(int i=0;i<m.row;i++){
		for(int j=0;j<m.col;j++){
			cout<<"Enter "<<"["<<i<<"]["<<j<<"]"<<" value: ";
			in>>m.pointer[i][j];
		}
	}
	return in;
}
Crashing when working with arrays is mostly due to two reasons: accessing restricted memory cells and memory leakage (I'm not 100% if the terms are technically correct).

You only used delete in the destructor, what about the functions that return a matrix? Don't you want to delete your temporary matrix?

Addition and subtractions work without the delete, I would imagine that multiplication should work the same way. I also don't recall reading in the book I am going through right now anything about having to delete anything from each overloaded function.

You're right I wanted to ask whether addition and subtraction crashed your application. Anyway the algorithm seems right, and I even tried rewriting the multiplication manually inside main and without using classes, there was no problem.

Also, I don't get how you're assuming values are initialized to 0. And, how didn't this give you compile errors?
Matrix operator=(Matrix& m)

It should be like this

Matrix & operator=(const Matrix& m)
Last edited on
Does multiplication work for you only inside main? How about the function?

Mike
Okay, I've been testing for the last hour now :D

I fixed something:

1
2
3
4
5
6
for(i=0;i<solution.row;i++){
		for(j=0;j<solution.col;j++){
			for(k=0;k<m.row;k++){
					solution.pointer[i][j]+=pointer[i][k] * m.pointer[k][j];////PROBLME HERE ON THIS LINE!!!!!!!!!!!!!!!!
                    cout << "!" << pointer[i][k] << " " << m.pointer[k][j] <<
                    endl;


And I make sure the multiplication operator returns correctly. For some reason, there seems to be a whooole lot of integers in the io buffer that come up all at once when using any cout... Couldn't figure out why, and I probably won't because I never use the new[] operator...

If your only interested in the algorithm itself rather than practicing C++ dynamic arrays, go ahead and use vector in STL.
Last edited on
Thanks a lot for the help. I was looking however to returning the object, but what you suggest should be fine for now. I think with a little more experience, I will come back to this and try to figure it out again.

Mike
Topic archived. No new replies allowed.