Answer to Question #315163 in C++ for khan

Question #315163

Implement a class Matrix to create matrices of 2x2, 3x3 and 4x4 with the following private data members:

1. int **matrix – a double pointer of type integer to create a 2D array (matrix)

2. int row – an integer to store the rows in the matrix

3. int col – an integer to store the columns in the matrix

The class shall have the following public member functions:

1. Matrix (int n1, int n2, int n3, int n4, int row = 2, int col = 2) – a parametrized

constructor for a 2x2 matrix

2. Matrix (int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9, int row = 3,

int col = 3) – a parametrized constructor for a 3x3 matrix

3. Matrix (int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9, int n10, int

n11, int n12, int n13, int n14, int n15, int n16, int row = 4, int col = 4) – a

parametrized constructor for a 4x4 matrix

4. Matrix(const Matrix &m) – a copy constructor

5. ~Matrix() – a destructor

6. int getRow() – a getter for rows

7. int getCol() – a getter for columns



1
Expert's answer
2022-03-21T12:35:57-0400
class Matrix {
private:
    int **matrix;
    int row;
    int col;


public:
    Matrix(int n1, int n2, int n3, int n4, int row = 2, int col = 2);
    Matrix (int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9, int row = 3,
            int col = 3);
    Matrix (int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9, int n10,
            int n11, int n12, int n13, int n14, int n15, int n16, int row = 4, int col = 4);
    Matrix(const Matrix &m);
    ~Matrix();
    int getRow() const;
    int getCol() const;
};


Matrix::Matrix(int n1, int n2, int n3, int n4, int row, int col) 
    : row(row), col(col)
{
    matrix = new int*[row];
    for (int i=0; i<row; i++) {
        matrix[i] = new int[col];
    }
    matrix[0][0] = n1;  matrix[0][1] = n2;
    matrix[1][0] = n3;  matrix[1][1] = n4;
}


Matrix::Matrix (int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9, int row, int col)
    : row(row), col(col)
{
    matrix = new int*[row];
    for (int i=0; i<row; i++) {
        matrix[i] = new int[col];
    }
    matrix[0][0] = n1;  matrix[0][1] = n2;  matrix[0][2] = n3;
    matrix[1][0] = n4;  matrix[1][1] = n5;  matrix[1][2] = n6;
    matrix[2][0] = n7;  matrix[2][1] = n8;  matrix[2][2] = n9;
}            


Matrix::Matrix (int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9, int n10,
                int n11, int n12, int n13, int n14, int n15, int n16, int row, int col)
    : row(row), col(col)
{
    matrix = new int*[row];
    for (int i=0; i<row; i++) {
        matrix[i] = new int[col];
    }
    matrix[0][0] = n1;  matrix[0][1] = n2;  matrix[0][2] = n3;  matrix[0][3] = n4;
    matrix[1][0] = n5;  matrix[1][1] = n6;  matrix[1][2] = n7;  matrix[1][3] = n8;
    matrix[2][0] = n9;  matrix[2][1] = n10; matrix[2][2] = n11; matrix[2][3] = n12;
    matrix[3][0] = n13; matrix[3][1] = n14; matrix[3][2] = n15; matrix[3][3] = n16;
}  


Matrix::Matrix(const Matrix &m) 
    : row(m.row), col(m.col)
{
    matrix = new int*[row];
    for (int i=0; i<row; i++) {
        matrix[i] = new int[col];
        for (int j=0; j<col; j++) {
            matrix[i][j] = m.matrix[i][j];
        }
    }


}
Matrix::~Matrix()
{
    for (int i=0; i<row; i++) {
        delete [] matrix[i];
    }
    delete matrix;
}


int Matrix::getRow() const
{
    return row;
}


int Matrix::getCol() const
{
    return col;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog