C++ program for addition and subtraction of two matrices
kw.cpp
#include <iostream>
using namespace std;
int main()
{
    int a[10][10], b[10][10], c[10][10], i, j, m, n;
    cout<<"———————————————————————————————————————————";
    cout<<"\nProgram for addition and subtraction of matrices\n";
    cout<<"———————————————————————————————————————————";
    cout<<"\nEnter the number of row(s) ";
    cin>>m;
    cout<<"Enter the number of column(s) ";
    cin>>n;
    if (m && n <= 0 || m * n <= 0)
    {
        cout<<"-------------------------------------------";
        cout<<"\nEnter the valid number of rows and columns";
        cout<<"\n-------------------------------------------\n";
    }
    else
    {
        cout<<"\nEnter elements for matrix A \n";
        for (i = 1; i <= m; i++)
        {
            for (j = 1; j <= n; j++)
            {
                cout<<"["<<i<<"]"
                    <<"["<<j<<"]=";
                cin>>a[i][j];
            }
            cout<<"\n";
        }

        cout<<"Enter elements for matrix B \n";
        for (i = 1; i <= m; i++)
        {
            for (j = 1; j <= n; j++)
            {
                cout<<"["<<i<<"]"
                    <<"["<<j<<"]=";
                cin>>b[i][j];
            }
            cout<<"\n";
        }

        cout<<"Matrix A is \n";
        for (i = 1; i <= m; i++)
        {
            for (j = 1; j <= n; j++)
            {
                cout<<"\t"<<a[i][j];
            }
            cout<<"\n";
        }

        cout<<"Matrix B is \n";
        for (i = 1; i <= m; i++)
        {
            for (j = 1; j <= n; j++)
            {
                cout<<"\t"<<b[i][j];
            }
            cout<<"\n";
        }

        cout<<"The addition of matrix A and matrix B is\n";
        for (i = 1; i <= m; i++)
        {
            for (j = 1; j <= n; j++)
            {
                c[i][j] = a[i][j] + b[i][j];
                cout<<"\t"<<c[i][j];
            }
            cout<<"\n";
        }

        cout<<"The subtraction of matrix A and matrix B is\n";
        for (i = 1; i <= m; i++)
        {
            for (j = 1; j <= n; j++)
            {
                c[i][j] = a[i][j] - b[i][j];
                cout<<"\t"<<c[i][j];
            }
            cout<<"\n";
        }
    }
    cout<<"———————————————————————————————————————————\n";
    return 0;
}
Output
kodingwindow@kw:~$ g++ kw.cpp
kodingwindow@kw:~$ ./a.out ——————————————————————————————————————————— Program for addition and subtraction of matrices ——————————————————————————————————————————— Enter the number of row(s) 2 Enter the number of column(s) 2 Enter elements for matrix A [1][1]=5 [1][2]=5 [2][1]=6 [2][2]=6 Enter elements for matrix B [1][1]=6 [1][2]=6 [2][1]=5 [2][2]=5 Matrix A is 5 5 6 6 Matrix B is 6 6 5 5 The addition of matrix A and matrix B is 11 11 11 11 The subtraction of matrix A and matrix B is -1 -1 1 1 ——————————————————————————————————————————— kodingwindow@kw:~$
What Next?
C++ Classes
Advertisement