Matrix is a 2 dimensional array of row size r and column size c. There are speci
ID: 3619559 • Letter: M
Question
Matrix is a 2 dimensional array of row size r and column size c. There are specific rules of adding or subtracting matrices A and B (of same row size m and column size n). The product of matrix A of dimension m by n (m is the row size or row dimension, n is the column dimension) and another matrix B of dimension p by q is meaningful only if p = n and in this case AB is a matrix of dimension m by q.Define class Matrix with at least three private variables: int rsize, csize; // row size, and column size; and double **ptr; // pointer to two dimensional array of matrix. Note to instantiate a two dimensional matrix M, in the constructor you will have code like ptr = new double [rsize][csize];
Note: you may use the following code in the constructor to get pointer arrays and then arrays (you’ll wite ptr [i][j] = 0.0; to initialize every entry).
ptr = new double *[csize];
for (int k = 0; k < rsize; k++)
ptr [k] = new double [rsize];
(b) Provide constructor when row size and column size are specified. (i.e. Matrix::Matrix(m, n); ). You will set all the arguments to 0.0. have default constructor for class Matrix that sets row dimension r and column dimension both to 2 when not specified.
Provide Add function that adds matrix B to matrix A (syntax; Matrix A (3, 5), B (3, 5); A.add(B);). After adding, A will be equal to A + B. Note you’ll provide error message if B has different dimensions from A such as adding B of dimension 4 by 6 to A of dimension 3 by 5.
Provide Subtract function that subtracts B from A.
Provide scalar multiplication function smul (double) like A.smul (x) which will multiply all the elements of A by the value x.
Provide multiplication function mul so that A.mul(B) is equal to AB if A’s column dimension c is equal to B’s row dimension. Generate error message if the product AB is meaningless.