Dynamic Matrix Class not Functioning Properly

Hello,
I am a Computer Engineering student and am in a data structures class. We recently had an assignment to create a matrix class and implement our own overloaded operator functions to peform basic arithmetic on it. My professor told me I was shallow copying instead of deep copying some of my data and my chaining does not work for addition (and probably other functions).

I'm really proud of this, it is my first class, and I'd like to perfect it.

If anybody out there has time to look over this code, I'd love to hear suggestions on how to optimize it so that I can learn more about the C++ language and have a usable class!

Thanks!

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
// Header file matrix.h for the class matrix.          
//*************************************************** 
//***************************************************
#include <iostream> 
#include <iomanip>
#include <stdlib.h> 

using namespace std; 

typedef int matrixData; 

class matrix 
{ 

// overloaded operator<<: provides ability to output matrices
	friend ostream &operator<< (ostream &out, matrix &output);
	
	public:
   		// Constructor: creates matrix as specified. 
   		matrix(int row_input, int column_input);
		//Copy constructor: creates a new matrix with the same data from a given matrix
		matrix(const matrix &another);
   
// Destructor: deallocates memory allocated in constructor 
   		~matrix(); 
   
// overloaded operators +, -, *, +=, -=, *=, ==, !=, and = for arithmetic and matrix tests
		matrix operator= (const matrix &another);
		matrix operator+ (const matrix &another);
		matrix operator+= (const matrix &another);
		matrix operator- (const matrix &another);
		matrix operator-= (const matrix &another);
		matrix operator* (const matrix &another);
		matrix operator*= (const matrix &another);
		bool operator== (const matrix &another);
		bool operator!= (const matrix &another);

private:
   		int row;            // the row size of the matrix    
		int column;         // the column size of the matrix
   		matrixData **data;  // dynamically allocated array storage          
}; // end class 

// End of header file. 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <iostream>
#include <iomanip>
#include "matrix.h"
#include <stdlib.h>

using namespace std;

//constructor
matrix::matrix(int row_input, int column_input)
{
	row = row_input;
	column = column_input;

	//exception handling for a matrix of less than 1X1
	try
	{
		if (row_input < 1 || column_input < 1)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cerr << "The matrix dimensions given are invalid.\n\n";
	}

	//memory allocated for array of pointers. 
	data = new int *[row_input];

	//exception handling if new == NULL
	try
	{
		if (data == NULL)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cout << "Memory allocation failed.\n\n";
	}
 
	//memory allocated for the two dimensional array. 
	for(int i = 0; i < row_input; i++) 
		data[i] = new int[column_input];

	//exception handling if new == NULL
	try
	{
		if (data == NULL)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cout << "Memory allocation failed.\n\n";
	}
 
	//creation of matrix
	for (int i = 0; i < row_input; i++)
		for (int j = 0; j < column_input; j++)
			data[i][j] = (matrixData) i - j;
}

//destructor
matrix::~matrix()
{
	//free the allocated memory
	for( int i = 0; i < row; i++ )
		delete [] data[i];   // Delete all cells in each row

	delete [] data; // Delete array of pointers
}

//copy constructor
matrix::matrix (const matrix &another)
{
	row = another.row;
	column = another.column;

	//memory allocated for array of pointers. 
	data = new int *[row];

	//exception handling if new == NULL
	try
	{
		if (data == NULL)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cout << "Memory allocation failed.\n\n";
	}

	//memory allocated for the two dimensional array. 
 	for(int i = 0; i < row; i++) 
		data[i] = new int[column];

	//exception handling if new == NULL
	try
	{
		if (data == NULL)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cout << "Memory allocation failed.\n\n";
	}

	//construction of matrix
	for (int i = 0; i < row; i++)
		for (int j = 0; j < column; j++)
			data[i][j] = another.data[i][j];
}

//operator= to act as a copy constructor
matrix matrix::operator= (const matrix &another)
{
	//make sure row and column are the same size as matrix you are trying to copy
	row = another.row;
	column = another.column;

	//copy data to the another matrix
	for (int i = 0; i < row; i++)
		for (int j = 0; j < column; j++)
			another.data[i][j] = data[i][j];

	return another;
}

//operator+ to add two matrices
matrix matrix::operator+ (const matrix &another)
{
	//exception handling to make sure matrices are of equal size
	try
	{
		if (row != another.row || column != another.column)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cerr << "The matrices were not equal in size.\n\n";
	}

	//matrix to hold the result
	matrix result(row, column);

	//loops to add two matrices together
	for (int i = 0; i < row; i++)
		for (int j = 0; j < column; j++)
			result.data[i][j] = data[i][j] + another.data[i][j];

	return result;
}

//operator+= to add two matrices and store that data in the original matrix
matrix matrix::operator+= (const matrix &another)
{
	//exception handling to make sure matrices are of equal size
	try
	{
		if (row != another.row || column != another.column)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cerr << "The matrices were not equal in size.\n\n";
	}
	
	//temp matrix to hold data
	matrix temp(row, column);


	//loops to store addition in a temp matrix
	for (int i = 0; i < row; i++)
		for (int j = 0; j < column; j++)
			temp.data[i][j] = data[i][j] + another.data[i][j];

	//loops to store addition in original matrix
	for (int i = 0; i < row; i++)
		for (int j = 0; j < column; j++)
			another.data[i][j] = temp.data[i][j];
				
	return another;
}

//operator- to define subtraction of two matrices
matrix matrix::operator- (const matrix &another)
{
	//exception handling to make sure matrices are of equal size
	try
	{
		if (row != another.row || column != another.column)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cerr << "The matrices were not equal in size.\n\n";
	}
	
	//matrix to hold the result
	matrix result(row, column);
	
	//loops to subtract the matrices and store their result
	for (int i = 0; i < row; i++)
		for (int j = 0; j < column; j++)
			result.data[i][j] = data[i][j] - another.data[i][j];

	return result;
}

matrix matrix::operator-= (const matrix &another)
{
	//exception handling to make sure matrices are of equal size
	try
	{
		if (row != another.row || column != another.column)
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cerr << "The matrices were not equal in size.\n\n";
	}
	
	//temporary matrix to hold the data
	matrix temp(row, column);

	//loops to subtract matrices
	for (int i = 0; i < row; i++)
		for (int j = 0; j < column; j++)
			temp.data[i][j] = data[i][j] - another.data[i][j];

	//loops to put the temp data into the original matrix
	for (int i = 0; i < row; i++)
		for (int j = 0; j < column; j++)
			another.data[i][j] = temp.data[i][j];
	
	return another;
}

//operator* to multiply two matrices
matrix matrix::operator* (const matrix &another)
{
	//exception handling to deal with two matrices that will not multiply due to their sizes
	try
	{
		if (!(column == another.row))
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cerr << "The matrices were not correctly sized for multiplication.\n\n";
	}

	//create a matrix to hold the results and temporary variable to hold the sum of each iteration
	matrix result(row, another.column);
	int tempSum = 0;
		
	//loops to allow you to move through the matrix in the correct order for matrix multiplication
	for(int i=0; i < row; i++)
	{
   		for(int j=0; j < another.column; j++)
		{
			tempSum = 0;
    		for(int k=0; k < column; k++)
			{
				tempSum += (data[i][k] * another.data[k][j]);
				result.data[i][j] = tempSum;
			}
		}
	}

	return result;
}

//operator* to multiply two matrices and copy the data into the original matrix
matrix matrix::operator*= (const matrix &another)
{
	//exception handling to deal with two matrices that will not multiply due to their sizes
	try
	{
		if (!(column == another.row))
			throw 1;
	}
	catch (int err)
	{
		cerr << "An error has occurred!\n";
		if (err == 1)
			cerr << "The matrices were not correctly sized for multiplication.\n\n";
	}
	
	//create a matrix to hold the results and temporary variable to hold the sum of each iteration
	matrix temp(column, another.row);
	int tempSum = 0;
		
	//loops to allow you to move through the matrix in the correct order for matrix multiplication
	for(int i=0; i < row; i++)
	{
   		for(int j=0; j < another.column; j++)
		{
			tempSum = 0;
    		for(int k=0; k < column; k++)
			{
				tempSum += (data[i][k] * another.data[k][j]);
				temp.data[i][j] = tempSum;
			}
		}
	}

	//loops to store data from the temporary matrix into the original matrix called by user
	for (int i = 0; i < another.row; i++)
		for (int j = 0; j < column; j++)
			another.data[i][j] = temp.data[i][j];

	return another;
}


//operator<< to output data in a matrix
ostream &operator<< (ostream &out, matrix &output)
{
	//only used to output a matrix of size 5X5
	//if statement limits this
	if (output.row <= 5 && output.column <= 5)
	{
		for (int i = 0; i < output.row; i++)
		{
			out << "\n";

			for (int j = 0; j < output.column; j++)
				out << output.data[i][j] << "\t";
		}
	
		return out;
	}
	//the else statement uses loops to limit the output of a matrix larger than 5X5 to 5X5
	else
	{
		for (int i = 0; i < 5; i++)
		{
			out << "\n";

			for (int j = 0; j < 5; j++)
				out << output.data[i][j] << "\t";
		}
	
		return out;
	}
}
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
// matrixMain.cpp
//Used to implement matrix class and describe
//to user how to use this program
//*******************************************
#include "matrix.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main()
{
	matrix a(3, 4);
	matrix b(3, 4); 
	matrix c(4, 4); 
	matrix d(4, 4);
	matrix e(4, 4);
	matrix f(4, 4);
	matrix h(4, 4);

	cout << "Welcome to the Matrix Class Implementation Program!\n"
		 << "This program will show the capabilities of the matrix class.\n"
		 << "Below you will see some matrices added, subtracted, multiplied, and tested for equality/inequality.\n"
		 << "Enjoy!\n\n";

	if ( a != b ) 
   {
   	 cout << "Matrix a and b are not equal" << endl;
     cout << "a = " << a << endl;
     cout << "b = " << b << endl;
   }
   else cout << "Matrix a and b are the same" << endl;
   	
   c *= d; 

   cout << "d = " << d << endl;
   cout << "c *= d = " << c << endl;
   
   e += f; 

   cout << "f = " << f << endl;
   cout << "e += f = " << e << endl;
   
   f = e = d;
   h = d + e + f;
   cout << "d = " << d << endl;
   cout << "e = " << e << endl;
   cout << "f = " << f << endl;
   cout << "h = " << h << endl;

   if ( h == e ) cout << "Matrix h and e are the same" << endl;
   else 
   {
   	 cout << "Matrix h and e are not equal" << endl;
     cout << "h = " << h << endl;
     cout << "e = " << e << endl;
   }
   
   return 0;
} 

Welcome to the Matrix Class Implementation Program!
This program will show the capabilities of the matrix class.
Below you will see some matrices added, subtracted, multiplied, and tested for e
quality/inequality.
Enjoy!

Matrix a and b are the same
d =
-14     -8      -2      4
-8      -6      -4      -2
-2      -4      -6      -8
4       -2      -8      -14
c *= d =
0       -1      -2      -3
1       0       -1      -2
2       1       0       -1
3       2       1       0
f =
0       -2      -4      -6
2       0       -2      -4
4       2       0       -2
6       4       2       0
e += f =
0       -1      -2      -3
1       0       -1      -2
2       1       0       -1
3       2       1       0
d =
0       -1      -2      -3
1       0       -1      -2
2       1       0       -1
3       2       1       0
e =
0       -1      -2      -3
1       0       -1      -2
2       1       0       -1
3       2       1       0
f =
0       -2      -4      -6
2       0       -2      -4
4       2       0       -2
6       4       2       0
h =
0       -1      -2      -3
1       0       -1      -2
2       1       0       -1
3       2       1       0
Matrix h and e are the same

Topic archived. No new replies allowed.