Answer to Question #132522 in C++ for KA

Question #132522
Write a C++ program to add two matrices and print it.
1
Expert's answer
2020-09-14T12:37:11-0400
#include<iostream>
using namespace std;
int main ()
{
    int m, n, p, q, i, j, A[5][5], B[5][5], C[5][5];
    cout <<"Enter rows and column of 1st matrix : ";
    cin >> m >> n;
    cout <<"Enter rows and column of 2nd matrix : ";
    cin >> p >> q;
    if ((m != p) && (n != q))
    {
        cout << "Matrices cannot be added!";
        exit(0);
    }
    cout << "Enter elements of 1st matrix : ";
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            cin >> A[i][j];
    cout << "Enter elements of 2nd matrix :  ";
    for (i = 0; i < p; i++)
        for (j = 0; j < q; j++)
            cin >> B[i][j];
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            C[i][j] = A[i][j] + B[i][j];
    cout << "Sum of matrices\n";
    for (i = 0; i < m; i++)
    {    for (j = 0; j < n; j++)
            cout << C[i][j] << "  ";
        cout << "\n";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS