Programming Assignment 8
In LAB 9 (10/30/20), you learned how to manipulate pointers to construct a matrix in an 1D array and run matrix multiplication over two matrices. Create a class called 'Matrix' containing constructor that initializes the number of rows and the number of columns of a new Matrix object.
The Matrix class has the following information:
1 - number of rows of matrix, represented by int type
2 - number of columns of matrix, represented by int type
3 - elements of matrix (use 1D pointer you learned from LAB9), represented by int* type
The Matrix class has functions for each of the following:
1 - get the number of rows
2 - get the number of columns
3 - set/get the element of the matrix at a given position (i, j)
4 - adding two matrices and return the result
5 - multiplying the two matrices and return the result
Complete the Matrix class based on the following code:
Class Matrix {
private:
int _rows, _cols; // # rows and columns of this matrix
int* _matrix; // 1D pointer to the 2D matrix, row-major
public:
// TODO: constructs the matrix using std::malloc based on
the given rows and cols
Matrix(int rows, int cols) {
}
// TODO: destructs the matrix using std::free to return the
Memory you requested at constructor
~Matrix() {
free(m_matrix);
}
// TODO: get the number of rows
int get_rows() {
}
// TODO: get the number of columns
int get_cols() {
}
// TODO: set the elements at (i, j) to a value
void set_element(int i, int j, int v) {
}
// TODO: get the value of element at (i, j)
int get_element(int i, int j) {
}
// TODO: add the given matrix to this matrix and return
a new matrix; DO NOT change the content of this matrix
Matrix add(const Matrix& given) {
}
// TODO: multiplies the given matrix by this matrix and return
The result to a new matrix; DO NOT change the content
of this matrix.
Matrix multiply(const Matrix& given) {
}
}