TutorialStudyMite

Addition of matrices in C++

JJuhi Kamdar2 min read
Beginner friendly

Track completion, mastery, and revision.

To add two matrices, we need two matrices having the same number of rows and columns. The addition is done by adding corresponding elements.

Algorithm:

  1. Take input of number of rows and columns.
  2. Take input of both the matrices.
  3. Now, use Ci,j= Ai,j + Bi,j
  4. Print matrix C.

Code:

#include <iostream>
using namespace std;
void subtract(int[5][5], int[5][5], int, int);
int display(int[5][5], int, int);
int main()
{

   int a[5][5], b[5][5], r1, c1, r2, c2;
   cout<<"\n Enter rows for first matrix: ";
    cin>>r1;
   cout<<"\n Enter columns for second matrix: ";
     cin>>c1;
   cout<<"\n Enter rows for first matrix: ";
     cin>>r2;
   cout<<"\n Enter columns for second matrix: ";
      cin>>c2;

  // To check if columns of first matrix are equal to rows of second matrix

   if (r1 != r2 || c1!=c2)
        return 0;

   // Storing elements of first matrix.

   printf("\nEnter elements of first matrix \n");

   for(int i=0; i<r1; i++)
    {
       for(int j=0; j<c1; j++)
           cin>>a[i][j];

    }
   // Storing elements of second matrix.
    cout<<"\nEnter elements of second matrix\n";

   for(int i=0; i<r2; i++)
    {
       for(int j=0; j<c2; j++)
           cin>>b[i][j];
    }
    cout<<"\n display a\n";
    display(a, r1, c1);
    cout<<"\n display a\n";
    display(b, r2, c2);
    //calling the function to add elements of a and b. passing number of rows
    //and columns in both of them
   add(a, b, r1, c1);
   return 0;
}

void add(int a[5][5], int b[5][5], int row, int col)
{
   int c[5][5];
    //input 0 for all values of c, in porder to remove
    //the garbage values assigned earlier
    for(int i=0; i<row; i++)
    {
       for(int j=0; j<col; j++)
           c[i][j]=0;
    }
    //we apply the same formula as above
   for(int i=0; i<row; i++)
    {
       for(int j=0; j<col; j++)
            c[i][j]=a[i][j]+b[i][j];
    }
    //to display matrix
 cout<<"\n Matrix c after Addition is:\n";
  display(c, row, col);
}
int display(int c[5][5], int row, int col)
{
   for(int i=0; i<row; i++)
   {
       for(int j=0; j<col; j++)
            cout<<c[i][j]<<" ";
        cout<<"\n";
    }
    return 0;
}

Output:

Enter rows for first matrix: 3
Enter columns for second matrix: 3
Enter rows for first matrix: 3
Enter columns for second matrix: 3
Enter elements of first matrix

76 35 24
24 56 24
86 25 75

Enter elements of second matrix

5 7 8
7 3 6
6 3 7

display a
76 35 24
24 56 24
86 25 75

display b
5 7 8
7 3 6
6 3 7

Matrix c after Addition is:
81 42 32
31 59 30
92 28 82

Finished reading?

Was this helpful?

Your feedback shapes better tutorials for everyone.