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

this is for an intro C++ class For this week’s assignment, you’ll be making a fu

ID: 3819440 • Letter: T

Question

this is for an intro C++ class

For this week’s assignment, you’ll be making a function that can not only add two matrices, but also multiply them, as well as a single matrix and a scalar.

Your function will need to correctly perform operations on the provided matrices and print out the resulting matrices.

Matrix A:

2

0

4

2

Matrix B:

1

3

2

0

Matrix C:

1

0

0

1

FOR YOUR ASSIGNMENT: You’ll be multiplying the provided matrices as follows:

(1) Matrix A * Matrix B

(2) Matrix B * Matrix A

(3) Matrix C * Matrix B

(4) Matrix A + Matrix B

(5) -4 * Matrix B

2

0

4

2

Explanation / Answer

// Example program
#include <iostream>
#include <string>
using namespace std;
int** add(int **a,int **b,int n,int m);
int** mul(int **a,int **b,int n,int m);
int** scal(int a,int **b,int n,int m);
int main()
{
std::string name;
std::cout << "What is your name? ";
//getline (std::cin, name);
std::cout << "Hello, " << name << "! ";
int n =2;
int m= 2;
int **MatA = new int*[n];
MatA[0] = new int[m];
MatA[1] = new int[m];
int **MatB = new int*[n];
MatB[0] = new int[m];
MatB[1] = new int[m];
int **MatC = new int*[n];
MatC[0] = new int[m];
MatC[1] = new int[m];

MatA[0][0] =2; MatA[0][1] =0;
MatA[1][0] =4; MatA[1][1] =2;

MatB[0][0] =1; MatB[0][1] =3;
MatB[1][0] =2; MatB[1][1] =0;

MatC[0][0] =1; MatC[0][1] =0;
MatC[1][0] =0; MatC[1][1] =1;

int **mat;
mat=mul(MatA,MatB,n,m);
cout<<"(1) Matrix A * Matrix B"<<endl;
cout <<" "<<mat[0][0]<<" "<<mat[0][1]<<endl;
cout <<" "<<mat[1][0]<<" "<<mat[1][1]<<endl<<endl;

mat=mul(MatB,MatA,n,m);
cout<<"(2) Matrix B * Matrix A"<<endl;
cout <<" "<<mat[0][0]<<" "<<mat[0][1]<<endl;
cout <<" "<<mat[1][0]<<" "<<mat[1][1]<<endl<<endl;

mat=mul(MatC,MatB,n,m);
cout<<"(3) Matrix C * Matrix B"<<endl;
cout <<" "<<mat[0][0]<<" "<<mat[0][1]<<endl;
cout <<" "<<mat[1][0]<<" "<<mat[1][1]<<endl<<endl;

mat=mul(MatA,MatB,n,m);
cout<<"(4) Matrix A + Matrix B"<<endl;
cout <<" "<<mat[0][0]<<" "<<mat[0][1]<<endl;
cout <<" "<<mat[1][0]<<" "<<mat[1][1]<<endl<<endl;

mat=scal(-4,MatB,n,m);
cout<<"(5) -4 * Matrix B"<<endl;
cout <<" "<<mat[0][0]<<" "<<mat[0][1]<<endl;
cout <<" "<<mat[1][0]<<" "<<mat[1][1]<<endl<<endl;
}

int** add(int **a,int **b,int n,int m){
    int ** c=new int*[n];
    for(int i=0;i<n;i++){
        c[i] = new int[m];
        for(int j=0;j<m;j++){
         c[i][j] = a[i][j] + b[i][j];
        }
    }
    return c;
}

int** mul(int **a,int **b,int n,int m){
    int ** c=new int*[n];
    for(int i=0;i<n;++i){
        c[i] = new int[m];
        for(int j=0;j<m;++j){
            for(int k=0; k<m; ++k)
            {
                c[i][j]+=a[i][k]*b[k][j];
            }
        }
    }
    return c;
}

int** scal(int a,int **b,int n,int m){
    int ** c=new int*[n];
    for(int i=0;i<n;i++){
        c[i] = new int[m];
        for(int j=0;j<m;j++){
         c[i][j] = a* b[i][j];
        }
    }
    return c;
}