In: Computer Science
<Using C++ programming.>
<Prof said that you use the example given below.>
Write a program to find (A*B)*10 +
(2*C+D) using class Matrix.
For member functions, use add() and mul().
Add()
-a,b,c are all m*n matrices.
-Add(a,b,c,m,n): cij = aij + bij
-Add(a,m,n,num): cij = aij + num
Mul()
-a is an m*n matrix, b is an n*p matrix, and c is an m*p matrix.
-mul(a,b,c,m,n): (Matrix Multiplication)
-mul(a,m,n,num): aij = aij * num
code in c++
(code to copy)
#include<bits/stdc++.h>
using namespace std;
//matrix class
class Matrix{
        public:
        int rowLength;
        int colLength;
        vector<vector<int>> arr2d;
        Matrix(int rowLength, int colLength):rowLength(rowLength), colLength(colLength){
                vector<vector<int>> arr(rowLength, vector<int>(colLength));
                arr2d=arr;
        }
        //fills the matrix with random values between start and end
        void fillRand(int start, int end){
                for(int i=0;i<arr2d.size();i++){
                        for(int j=0;j<arr2d[i].size();j++){
                                arr2d[i][j]=rand()%(end-start+1) + start;
                        }
                }
        }
        //this is to change value of matrix
        void put(int i, int j, int value){
                arr2d[i][j]=value;
        }
        //this is to retrieve value from matrix
        int get(int i, int j){
                return arr2d[i][j];
        }
        //this adds two matrix
        static Matrix add(Matrix A, Matrix B){
                //create matrix c
                Matrix C(A.rowLength, A.colLength);
                for(int i=0;i<A.rowLength;i++){
                        for(int j=0;j<A.colLength;j++){
                                C.put(i, j, A.get(i, j)+B.get(i, j));
                        }
                }
                return C;
        }
        //this adds constant to a  matrix
        static Matrix add(Matrix A, int num){
                //create matrix c
                Matrix C(A.rowLength, A.colLength);
                for(int i=0;i<A.rowLength;i++){
                        for(int j=0;j<A.colLength;j++){
                                C.put(i, j, A.get(i, j)+num);
                        }
                }
                return C;
        }
        //this multiplies two matrix
        static Matrix mul(Matrix A, Matrix B){
                //create matrix c
                Matrix C(A.rowLength, A.colLength);
                for (int i = 0; i < A.rowLength; i++)
                {
                        for (int j = 0; j < B.colLength; j++)
                        {
                                C.put(i,j,0);
                                for (int k = 0; k < A.colLength; k++)
                                {
                                        C.put(i,j,C.get(i,j)+(A.get(i,k)*B.get(k,j)));
                                }
                        }
                }
                return C;
        }
        
        //this multiplies constant to a  matrix
        static Matrix mul(Matrix A, int num){
                //create matrix c
                Matrix C(A.rowLength, A.colLength);
                for(int i=0;i<A.rowLength;i++){
                        for(int j=0;j<A.colLength;j++){
                                C.put(i, j, A.get(i, j)*num);
                        }
                }
                return C;
        }
        //prints the matrix
        void print(string identifier){
                cout<<identifier+": "<<endl;
                for(int i=0;i<rowLength;i++){
                        for(int j=0;j<colLength;j++){
                                cout<<arr2d[i][j]<<" ";
                        }
                        cout<<endl;
                }
                cout<<endl;
        }
};
// driver function 
int main()
{
        /*
        We need to perform (A*B)*10 + (2*C+D)
        So we need to follow these constraints
        column length of A should be equal to rowlength of B
        row and column length of C should be equal to row and column of D 
        row of A and column of B should be equal to row of C/D and column of C/D
        */
        // lets take an example
        Matrix A(3, 4); //A is a 3*4 matrix
        Matrix B(4,4); //B is a 4 *4 matrix
        Matrix C(3,4); //C is a 3*4 matrix
        Matrix D(3,4); //D is a 3*4 matrix
        //populate matrix with random values between 10 and 20
        A.fillRand(10, 20);
        B.fillRand(10, 20);
        C.fillRand(10, 20);
        D.fillRand(10, 20);
        // this represents result = (A*B)*10 + (2*C+D)
        Matrix result = Matrix::add(Matrix::mul(Matrix::mul(A, B), 10), Matrix::add(Matrix::mul(C, 2), D));
        //print input and output
        A.print("A");
        B.print("B");
        C.print("C");
        D.print("D");
        result.print("(A*B)*10 + (2*C+D)");
}
code screenshot

Sample output screenshot
