Print subtraction of two matrices - C++ Program

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

Answer:

Following program is displaying subtraction 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 Difference 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]<<"\t";
                }
                cout<<"\n";
        }
        return 0;
}


Output:

two dimensional subtraction

Explanation:

subtraction of two matrices

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

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