Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Section: You are given a DynamicMatrix.h header file and an incomplete DynamicMa

ID: 3915896 • Letter: S

Question





Section: You are given a DynamicMatrix.h header file and an incomplete DynamicMatrix.cpp source file. You are required to implement a Parametrized Constructor and a Destructor for this class, such that: a) the Constructor allocates a dynamic 2D array to be used by the data member m_matrix of the class. It takes in only 2 parameters, the desired rows and columns of the matrix. You are not required (but you are encouraged) to implement this with allocation checking). b) the Destructor deallocates the dynamic 2D array data (the m matrix). The insertion operator overload implementation is already given. You are also give a lab8.cpp test source code file (and a Makefile) to test your code. I. Sample input and output Please input the rows and then the columns of the DynamicMatrix object Dynamic Matrix of 3 rows and 6 cols: (zeros here can also be any randomly initialized integer numbers)

Explanation / Answer

DynamicMatric::DynamicMatrix(int rows, int cols){

   m_rows = rows;
   m_cols = cols;

   m_matrix = new int *[rows];
   for (int i = 0; i<rows; i++)
        m_matrix[i] = new int[cols];

   for (int i = 0; i<m_rows; i++){
       for (int j = 0; j<m_cols; j++)
           m_matrix[i][j] = 0;
   }
}

DynamicMatric::~DynamicMatrix(){

   for (int i = 0; i<m_rows; i++)
        delete [] m_matrix[i];

   delete [] m_matrix;
}