Addition and Subtraction of Two Matrices in C++

Matrix Addition and Subtraction are very simple operations applied to Matrices in which we add/subtract corresponding terms of the matrices to obtain the resultant matrix. There is no complicated operation and the process is fairly simple. There are two matrices required of exactly equal dimensions. These matrices are stored in two dimensional arrays of integers. The entering of numbers is done through the 2 for loops. The for loop is iterated by keeping in mind the number of rows first followed by the number of columns. When finding the result, the same skeleton for loop is run but this time adds the corresponding elements of the matrix together to give the resultant matrix. Watch the code below:



#include <iostream>

using namespace std;

int main()
{
    int m, n, x, y, MatA[100][100], MatB[100][100];
    int Result[100][100];

    cout<<"Enter the number of rows and columns of matrices \n";
    cin>>m>>n;
    cout<<"Enter the elements of Matrix A\n";

    for ( x = 0 ; x < m ; x++ )
   {
       for ( y = 0 ; y < n ; y++ )
        {
            cin>>MatA[x][y];
        }
   }


   cout<<"Enter the elements of Matrix B\n";

   for ( x = 0 ; x < m ; x++ )
   {
       for ( y = 0 ; y < n ; y++ )
        {
            cin>>MatB[x][y];
        }
   }


   for ( x = 0 ; x < m ; x++ )
   {
       for ( y = 0 ; y < n ; y++ )
       {
          Result[x][y] = MatA[x][y] + MatB[x][y];
       }
   }

   cout<<"Resultant Matrix after Addition- \n";

   for ( x = 0 ; x < m ; x++ )
   {
      for ( y = 0 ; y < n ; y++ )
         cout<<"     "<<Result[x][y];

      cout<<"\n";
   }
  
   return 0;
}


To subtract the Matrices, we simply change one sign during the calculation i.e. +(plus sign) to -(minus sign). See code below:


#include <iostream>

using namespace std;

int main()
{
    int m, n, x, y, MatA[100][100], MatB[100][100];
    int Result[100][100];

    cout<<"Enter the number of rows and columns of matrices \n";
    cin>>m>>n;
    cout<<"Enter the elements of Matrix A\n";

    for ( x = 0 ; x < m ; x++ )
   {
       for ( y = 0 ; y < n ; y++ )
        {
            cin>>MatA[x][y];
        }
   }

   cout<<"Enter the elements of Matrix B\n";

   for x = 0 ; x < m ; x++ )
   {
       for y = 0 ; y < n ; y++ )
        {
            cin>>MatB[x][y];
        }
   }


   for x = 0 ; x < m ; x++ )
   {
       for y = 0 ; y < n ; y++ )
       {
          Result[x][y] = MatA[x][y] - MatB[x][y];
       }
   }

   cout<<"Resultant Matrix after Subtraction- \n";

   for ( x = 0 ; x < m ; x++ )
   {
      for ( y = 0 ; y < n ; y++ )
         cout<<"     "<<Result[x][y];

      cout<<"\n";
   }
  
   return 0;
}


Download Source Code Here for Matrix Addition and Here for Matrix Subtraction. Notice that the C++ Souce Code is written in Code::Blocks 12.11 IDE available for direct download Here.

Please ask your questions and list your suggestions below in the comments section. 

No comments:

Post a Comment

Custom Search