Print addition of two matrices - C++ Program

Q. Write a C++ program to print the addition of two matrices.

Answer:

Following program is displaying the addition of two matrices.

#include<iostream>
using namespace std;
int main()
{
        int m1[3][3], m2[3][3], i, j, m3[3][3];
        cout<<"\n Enter First Matrix Elements : \n";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        cout<<" ";
                        cin>>m1[i][j];
                }
        }
        cout<<"\n Enter Second Matrix Elements : \n";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        cout<<" ";
                        cin>>m2[i][j];
                }
        }
        cout<<"\n Sum of Two Matrices : \n\n";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        m3[i][j]=m1[i][j]+m2[i][j];
                }
        }
        for(i=0; i<3; i++)
        {
                cout<<" ";
                for(j=0; j<3; j++)
                {
                        cout<<m3[i][j]<<" ";
                }
                cout<<"\n";
        }
        return 0;
}


Output:

two dimensional addition

Explanation:

addition of two matrices

The above figure shows the work flow or structure of matrix and how actually it works. The above figure represents addition of two matrices.

After addition of two matrices, the result will display in the third matrix as shown in the above diagram.