Addition of Matrix in C++

Hello everyone! We can learn how to add two matrixes in C++ language in this article. Matrix consist of many elements that are arranged in the order of rows and columns. Generally addition takes place in matrix between the elements present in the matrix.

For example, consider two matrix, a=[a11 a12] b[11 b12] Then the addition takes place in below manner. a+b=[a11+b11 a12+b12]. For loop iterates to add the elements of two matrixes. You have to understand how for loop is used in the addition of two matrixes.

Addition of Matrix in C++

Addition of Matrix in C++

This program only allows rows and columns between 0 to 100. For loop iterates to take elements in the two matrix. sum[i][j] = a[i][j] + b[i][j]; adds the two matrix elements and displays the output.

#include <iostream>
using namespace std;

int main()
{
    int r, c, a[100][100], b[100][100], sum[100][100], i, j;

    cout << "Enter number of rows (between 1 and 100): ";
    cin >> r;

    cout << "Enter number of columns (between 1 and 100): ";
    cin >> c;

    cout << endl << "Enter elements of 1st matrix: " << endl;

    // Storing elements of first matrix entered by user.
    for(i = 0; i < r; ++i)
       for(j = 0; j < c; ++j)
       {
           cout << "Enter element a" << i + 1 << j + 1 << " : ";
           cin >> a[i][j];
       }

    // Storing elements of second matrix entered by user.
    cout << endl << "Enter elements of 2nd matrix: " << endl;
    for(i = 0; i < r; ++i)
       for(j = 0; j < c; ++j)
       {
           cout << "Enter element b" << i + 1 << j + 1 << " : ";
           cin >> b[i][j];
       }

    // Adding Two matrices
    for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j)
            sum[i][j] = a[i][j] + b[i][j];

    // Displaying the resultant sum matrix.
    cout << endl << "Sum of two matrix is: " << endl;
    for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j)
        {
            cout << sum[i][j] << "  ";
            if(j == c - 1)
                cout << endl;
        }

    return 0;
}
Read Also : C++ Program to Swap Two Numbers

Output

The output screen asks you to enter the rows and columns of the two matrix. Then the screen asks you to enter the elements of the matrix. Finally, it displays the output.

Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 2

Enter elements of 1st matrix:
Enter element a11: -4
Enter element a12: 5
Enter element a21: 6
Enter element a22: 8

Enter elements of 2nd matrix:
Enter element b11: 3
Enter element b12: -9
Enter element b21: 7
Enter element b22: 2

Sum of two matrix is:
-1   -4
13   10

Final Words

I hope this article helps you in the Addition of Matrix in C++ Program. If you face any issues please let me know via the comment section. Share this article with other C++ program developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment