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!
#include <iostream>
#include <iomanip>
#include "matrix.h"
#include <stdlib.h>
usingnamespace 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 = newint *[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] = newint[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 = newint *[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] = newint[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;
}
}
// 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>
usingnamespace 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