Question #38081

Design, develop and execute a program in read two A (MXN) to Q) and compute the product ofA and B ifthe matrices are compatible for The multiplication. program must print the input matrices and the resultant matrix with suitable headings and format if the matrices are compatible for multiplication, otherwise the program must print a be suitable (For the purpose ofdemonstration, the array sizes MMP, and Q can all message. less than or equal to 3)

help meeeee

Expert's answer

#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
    int m=0, n1=0, n2=0, k=0;
    cout<<"Input the size of the matrix N:"<<endl<<"Rows: "; cin>>m;
    cout<<endl<<"Columns: "; cin>>n1; cout<<endl;
    cout<<"Input the size of the matrix M:"<<endl<<"Rows: "; cin>>n2;
    cout<<endl<<"Columns: "; cin>>k; cout<<endl;
    if(n2!=n1)
    {
        cout<<"The number of columns do not correspond to the number of rows in the matrices N and M."<<endl<<"Multiplying matrix N by matrix M is not possible!"<<endl;
        cout<<"To exit the program, press any letter and Enter!";
        cin>>m;
        return 0;
    }
    else{
        cout<<endl<<"Matrix multiplication is possible!"<<endl;
        vector<vector<int> > N(m, vector<int>(n1));
        vector<vector<int> > M(n1, vector<int>(k));
        vector<vector<int> > Q(m, vector<int>(k));
        cout<<endl<<"Enter the matrix N: "<<endl;
        for(int ki=0; ki<m; ki++)
        {
            for(int kj=0; kj<n1; kj++)
            {
                cin>>N[ki][kj];
            }
            cout<<endl;
        }
        cout<<endl<<"Enter the matrix M:"<<endl;
        for(int ki=0; ki<n1; ki++)
        {
            for(int kj=0; kj<k; kj++)
            {
                cin>>M[ki][kj];
            }
            cout<<endl;
        }
        for(int ki=0; ki<m; ki++)
        {
            for(int kj=0; kj<k; kj++)
            {
                Q[ki][kj]=0;
                for(int kk=0; kk<n1; kk++)
                {
                    Q[ki][kj]+=N[ki][kk]*M[kk][kj];
                }
            }
        }
        cout<<endl<<"The product of the matrices N and M is:"<<endl;
        for(int ki=0; ki<m; ki++)
        {
            for(int kj=0; kj<k; kj++)
            {
                cout<<Q[ki][kj]<<" ";
            }
            cout<<endl;
        }
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS