C++ Long Programming Challenge Excercise. Need help implementening header file a
ID: 3883631 • Letter: C
Question
C++ Long Programming Challenge Excercise. Need help implementening header file and .cpp implementation.
Class defined is:
No makefile necessary
The Header file:
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix(unsigned r, unsigned c);
Matrix(const Matrix& rhs);
~Matrix();
// Insert overloaded = operator signature
// Access the individual elements of a matrix: insert overloaded operator signatures
// Matrix mathematical operations: insert overloaded operator signatures
// Linear equations: insert overloaded operator signatures
// Getters and setters:
unsigned getRows() const; // Return number of rows
unsigned getCols() const; // Return number of columns
private:
// Insert helper function signatures if necessary
double ** matrix; // the matrix array
unsigned rows; // # rows
unsigned cols; // # columns
};
#endif
6.1 Task 1: Implementing the Matrix File matrix.h contains an incomplete header of the Matrix class that you will implement for this task. A two-dimensional dynamic array is used to store the matrix data. The dimensions of the array are stored in two member variables: rows and cols, which stand for rows and columns, respectively. 6.1.1 Constructors Matrix constructor takes two unsigned int parameters, representing the number of rows and columns in the matrix. Upon receiving the dimension parameters, the constructor must allocate a dynamic array of the given size, and fill it with zeros. Remember to implement a destructor to deallocate the memory when the program exits. In addition to the constructor, implement an appropriate copy constructor, and overload the assignment operator to enable assigning Matrix objects to one an otherExplanation / Answer
Implemented the Matrix class according to the specifications in the question. Since the Guassian method is not known to me and the method is not described in the question, the operator | and |= are not implemented. All other functionality as mentioned in the question is done and tested. Hope the answer helped. If it did, please don't forget to rate it. Thank you very much.
matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix(unsigned r, unsigned c);
Matrix(const Matrix& rhs);
~Matrix();
// Insert overloaded = operator signature
Matrix& operator = (const Matrix &rhs);
// Access the individual elements of a matrix: insert overloaded operator signatures
Matrix operator [](int row);
double& operator ()(int row, int col) const;
friend ostream& operator << (ostream &out, const Matrix &mat);
friend istream& operator >> (istream &in, Matrix &mat);
// Matrix mathematical operations: insert overloaded operator signatures
Matrix operator * (double scalar);
friend Matrix operator * (double scalar, const Matrix &mat);
Matrix& operator *=(double scalar);
Matrix operator /(double scalar);
Matrix operator + (const Matrix &other );
Matrix& operator += (const Matrix &other);
Matrix operator - (const Matrix &other );
Matrix& operator -= (const Matrix &other);
Matrix operator * (const Matrix &other );
Matrix& operator *= (const Matrix &other);
Matrix operator ^ (int n);
Matrix& operator ^= (int n);
Matrix operator ~ ();
// Linear equations: insert overloaded operator signatures
// Getters and setters:
unsigned getRows() const; // Return number of rows
unsigned getCols() const; // Return number of columns
private:
// Insert helper function signatures if necessary
void deleteMatrix();
double ** matrix; // the matrix array
unsigned rows; // # rows
unsigned cols; // # columns
};
#endif
matrix.cpp
#include "matrix.h"
Matrix::Matrix(unsigned r, unsigned c)
{
rows = r;
cols = c;
matrix = new double*[rows];
for(int i = 0; i < rows; i++)
{
matrix[i] = new double[cols];
for(int j = 0; j < cols; j++)
matrix[i][j] = 0;
}
}
Matrix::Matrix(const Matrix& rhs)
{
matrix = NULL;
*this = rhs;
}
void Matrix::deleteMatrix()
{
if(matrix == NULL) return;
for(int i = 0; i < rows; i++)
delete []matrix[i];
delete []matrix;
rows = cols = 0;
matrix = NULL;
}
Matrix::~Matrix()
{
deleteMatrix();
}
// Insert overloaded = operator signature
Matrix& Matrix::operator = (const Matrix &rhs)
{
if(this != &rhs) //avoid self copy
{
deleteMatrix();
rows = rhs.rows;
cols = rhs.cols;
matrix = new double*[rows];
for(int i = 0; i < rows; i++)
{
matrix[i] = new double[cols];
for(int j = 0; j < cols; j++)
matrix[i][j] = rhs.matrix[i][j];
}
}
return *this;
}
// Access the individual elements of a matrix: insert overloaded operator signatures
Matrix Matrix::operator [](int row)
{
if(row >= rows)
throw "Error: invalid row index";
Matrix mat(1, cols);
for(int i = 0; i < cols; i++)
mat.matrix[0][i] = matrix[0][i];
return mat;
}
double& Matrix::operator ()(int row, int col) const
{
if(row >= rows)
throw "Error: invalid row index";
if(col >= cols)
throw "Error: invalid col index";
return matrix[row][col];
}
ostream& operator << (ostream &out, const Matrix &mat)
{
for(int i = 0; i < mat.getRows(); i++)
{
out << endl;
for(int j = 0; j < mat.getCols(); j++)
out << mat(i,j) << " ";
}
out << endl;
return out;
}
istream& operator >> (istream &in, Matrix &mat)
{
for(int i = 0; i < mat.getRows(); i++)
{
for(int j = 0; j < mat.getCols(); j++)
in >> mat(i,j);
}
return in;
}
// Matrix mathematical operations: insert overloaded operator signatures
Matrix Matrix::operator * (double scalar)
{
return scalar * (*this);
}
Matrix operator *(double scalar, const Matrix &mat)
{
Matrix result(mat.getRows(), mat.getCols());
for(int i = 0; i < result.rows; i++)
{
for(int j = 0; j < result.cols; j++)
{
result.matrix[i][j] = mat.matrix[i][j] * scalar;
}
}
return result;
}
Matrix& Matrix::operator *=(double scalar)
{
*this = (*this) * scalar;
return *this;
}
Matrix Matrix::operator /(double scalar)
{
if(scalar == 0.0)
throw "Error: division by zero";
Matrix result(rows, cols);
for(int i = 0; i < result.rows; i++)
{
for(int j = 0; j < result.cols; j++)
{
result.matrix[i][j] = matrix[i][j] / scalar;
}
}
return result;
}
Matrix Matrix::operator + (const Matrix &other )
{
if(rows != other.rows || cols != other.cols)
throw "Error: adding matrices of different dimensions";
Matrix result(rows, cols);
for(int i = 0; i < result.rows; i++)
{
for(int j = 0; j < result.cols; j++)
{
result.matrix[i][j] = matrix[i][j] + other.matrix[i][j];
}
}
return result;
}
Matrix& Matrix::operator += (const Matrix &other)
{
*this = (*this) + other;
return *this;
}
Matrix Matrix::operator - (const Matrix &other )
{
if(rows != other.rows || cols != other.cols)
throw "Error: subtracting matrices of different dimensions";
Matrix result(rows, cols);
for(int i = 0; i < result.rows; i++)
{
for(int j = 0; j < result.cols; j++)
{
result.matrix[i][j] = matrix[i][j] - other.matrix[i][j];
}
}
return result;
}
Matrix& Matrix::operator -= (const Matrix &other)
{
*this = (*this) - other;
return *this;
}
Matrix Matrix::operator * (const Matrix &other )
{
if(cols != other.rows)
throw "Error: invalid matrix dimensions";
Matrix result(rows, other.cols);
for(int i = 0; i < result.rows; i++)
{
for(int j = 0; j < other.cols; j++)
{
result.matrix[i][j] = 0;
for(int k = 0; k < cols; k++)
result.matrix[i][j] += matrix[i][k] * other.matrix[k][j];
}
}
return result;
}
Matrix& Matrix::operator *= (const Matrix &other)
{
*this = (*this) * other;
return *this;
}
Matrix Matrix::operator ^ (int n )
{
if(n <= 0)
throw "Error: negative power is not supported";
if(rows != cols)
throw "Error: non-square matrix provided";
Matrix self = *this;
Matrix result = self;
for(int i = 2; i <= n; i++)
result *= self;
return result;
}
Matrix& Matrix::operator ^= (int n)
{
*this = (*this) ^ n;
return *this;
}
Matrix Matrix::operator ~ ( )
{
Matrix result(cols, rows);
for(int i = 0; i < result.rows; i++)
for(int j = 0; j < result.cols; j++)
result.matrix[i][j] = matrix[j][i];
return result;
}
// Getters and setters:
unsigned Matrix::getRows() const // Return number of rows
{
return rows;
}
unsigned Matrix::getCols() const // Return number of column
{
return cols;
}
matrixtest.cpp
#include "matrix.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
Matrix A(2,3);
Matrix B(3,3);
//demonstrate >> and << operators
cout << "Enter matrix A of dimension 2x3" << endl;
cin >> A;
cout << "Enter matrix B of dimension 3x3" << endl;
cin >> B;
cout << "Matrix A of size " << A.getRows() << "x" << A.getCols() << endl;
cout << A;
cout << "Matrix B of size " << B.getRows() << "x" << B.getCols() << endl;
cout << B;
Matrix C = A * 5;
cout << "Scalar multiplication Matrix C = A * 5 " << endl;
cout << C << endl;
Matrix D = B ^ 2;
cout << "D = B ^ 2 " << endl;
cout << D << endl;
Matrix E = ~A;
cout << "E = ~A" << endl << E << endl;
cout << "B - B" << endl << (B - B) << endl;
cout << " B / 2" << endl << ( B/2 ) << endl;
Matrix F = A[1];
cout << "F = A[1] " << endl;
cout << F << endl;
cout << "Changing B(0,0) = 12 and B(2,0) = 14" << endl;
B(0,0) = 12;
B(2,0) = 14;
cout << "B = " << endl;
cout << B << endl;
try
{
Matrix G = A / 0;
cout << "G = " << endl << G << endl;
}
catch(const char * msg)
{
cout << msg << endl;
}
try
{
Matrix H = A * A;
cout << " H = A * A" << endl << H << endl;
}
catch(const char * msg)
{
cout << msg << endl;
}
}
output
Enter matrix A of dimension 2x3
1 2 3
4 5 6
Enter matrix B of dimension 3x3
7 8 9
0 1 2
4 5 6
Matrix A of size 2x3
1 2 3
4 5 6
Matrix B of size 3x3
7 8 9
0 1 2
4 5 6
Scalar multiplication Matrix C = A * 5
5 10 15
20 25 30
D = B ^ 2
85 109 133
8 11 14
52 67 82
E = ~A
1 4
2 5
3 6
B - B
0 0 0
0 0 0
0 0 0
B / 2
3.5 4 4.5
0 0.5 1
2 2.5 3
F = A[1]
1 2 3
Changing B(0,0) = 12 and B(2,0) = 14
B =
12 8 9
0 1 2
14 5 6
Error: division by zero
Error: invalid matrix dimensions
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.