Implement a Matrix class that supports a matrix of integers. The minimal interfa
ID: 3548001 • Letter: I
Question
Implement a Matrix class that supports a matrix of integers. The minimal interface for the
Matrix class is:
1. Matrix (max_number_rows, max_number_cols, initial_value ) - Implement a constructor
that initializes its data members and acquire the storage for dynamic two- dimensional array. The
dynamic memory should be initialized to the provided initial value. This constructor should
server as the default constructor. By default, the max_number_rows and the max_number_cols
should equal 1 while the initial_value should equal 0.
2. Clear( )
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
class Matrix
{
public:
// default constructor
Matrix(int max_number_rows = 1, int max_number_cols = 1, int initial_value = 0);
// copy constructor
Matrix(const Matrix& m);
// destructor
~Matrix();
// assign operator
Matrix& operator=(const Matrix& m);
// functions
void Clear();
int Rows() const;
int Columns() const;
int GetCell(int x, int y) const;
bool SetCell(int x, int y, int val);
void Debug(ostream& os);
friend istream& operator>>(istream& in, Matrix & m);
friend ostream& operator<<(ostream& out, const Matrix & m);
private:
int numRows;
int numCols;
int initVal;
int** data;
};
Matrix::Matrix(int max_number_rows, int max_number_cols, int initial_value)
{
numRows = max_number_rows;
numCols = max_number_cols;
initVal = initial_value;
data = new int*[numRows];
for (int i = 0; i < numRows; i++)
{
data[i] = new int[numCols];
for (int j = 0; j < numCols; j++)
data[i][j] = initial_value;
}
}
Matrix::Matrix(const Matrix& m)
{
numRows = numCols = 0;
data = NULL;
(*this) = m;
}
Matrix::~Matrix()
{
for (int i = 0; i < numRows; i++)
delete[] data[i];
delete[] data;
}
Matrix& Matrix::operator=(const Matrix& m)
{
if (this == &m)
return *this;
for (int i = 0; i < numRows; i++)
delete[] data[i];
delete[] data;
numRows = m.numRows;
numCols = m.numCols;
initVal = m.initVal;
data = new int*[numRows];
for (int i = 0; i < numRows; i++)
{
data[i] = new int[numCols];
for (int j = 0; j < numCols; j++)
data[i][j] = m.data[i][j];
}
return *this;
}
void Matrix::Clear()
{
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
data[i][j] = initVal;
}
}
int Matrix::Rows() const
{
return numRows;
}
int Matrix::Columns() const
{
return numCols;
}
int Matrix::GetCell(int x, int y) const
{
if (x < 0 || x >= numRows || y < 0 || y >= numCols)
return 0;
return data[x][y];
}
bool Matrix::SetCell(int x, int y, int val)
{
if (x < 0 || x >= numRows || y < 0 || y >= numCols)
return false;
data[x][y] = val;
return true;
}
void Matrix::Debug(ostream& os)
{
os << "Number of rows = " << numRows << endl;
os << "Number of columns = " << numCols << endl;
os << "Initializer = " << initVal << endl;
os << "Current contents of the matrx:" << endl;
os << (*this);
}
istream& operator>>(istream& in, Matrix & m)
{
for (int i = 0; i < m.numRows; i++)
{
for (int j = 0; j < m.numCols; j++)
{
in >> m.data[i][j];
}
}
return in;
}
ostream& operator<<(ostream& out, const Matrix & m)
{
for (int i = 0; i < m.numRows; i++)
{
for (int j = 0; j < m.numCols; j++)
{
out << setw(5) << m.data[i][j] ;
}
out << endl;
}
return out;
}
int main()
{
Matrix m1(3, 2, 11);
m1.Debug(cout);
Matrix m2;
m2.Debug(cout);
m2 = m1;
m2.Debug(cout);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.