codescracker
c++

C++ Program to Add Two Matrices



« Previous Program Next Program »

In this article, you will learn and get code on addition of two matrices in C++. There are two programs available here on matrix addition:

In both the cases, the elements of matrix must be entered by user at run-time.

How Two Matrix can be Added ?

The separate article is created for matrix addition. If you're not aware about it, then I recommend you to follow the article for complete understanding about it in very short time.

But for now, let me tell you about it, i.e., to add two matrices, we have to add corresponding elements. That is, element at first row and first column of first matrix gets added with the element at first row and first column of second matrix. In similar way, the element at first row and second column of first matrix gets added with the element at first row and second column of second matrix. And so on.

To add two matrices in C++ programming, you have to ask from user to enter the elements of both the matrix. Now add the same positioned elements to form a new matrix. After adding two matrices, display the third matrix which is the addition result of two matrices as shown in the following program.

Addition of Two 3*3 Matrix

This program asks user to enter any 3*3 two matrices. That is, user is allowed to enter 9 elements for first and 9 elements for second matrix. And then add them up to print the third matrix that will be the addition result of given two:

#include<iostream>
using namespace std;
int main()
{
    int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
    cout<<"Enter Elements of First Matrix: ";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cin>>mat1[i][j];
    }
    cout<<"Enter Elements of Second Matrix: ";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cin>>mat2[i][j];
    }
    cout<<"\nAdding the Two Given Matrix...\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            mat3[i][j] = mat1[i][j]+mat2[i][j];
    }
    cout<<"Addition Result of Two Given Matrix is:\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cout<<mat3[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program to add two matrices

Now enter any 9 numbers say 1, 2, 3, 4, 5, 6, 7, 8, 9 as the elements of first matrix and then again 9 numbers say 9, 8, 7, 6, 5, 4, 3, 2, 1 as elements of second matrix. Press ENTER key to see the addition result of two given matrix as shown in the output given below:

matrix addition c++

When user enters 9 elements of first matrix, then it gets stored in a way that:

Similarly, 9 elements of second matrix also gets stored in similar way. And the addition of two matrix goes in a way that:

Print the third matrix, that will be the summation result of two given matrix.

Allow User to Define Size

This program allows user to define the size of both the matrix.

Note - To define addition of two matrix, both the matrix must be of same order. Here same order means, row size of first matrix must be equal to the row size of second matrix and column size of first matrix must also be equal to the column size of second matrix.

#include<iostream>
using namespace std;
int main()
{
    int rowOne, colOne, rowTwo, colTwo, i, j;
    int mat1[10][10], mat2[10][10], sum=0;
    cout<<"Enter Row and Column Size of First Matrix: ";
    cin>>rowOne>>colOne;
    cout<<"Enter Row and Column Size of Second Matrix: ";
    cin>>rowTwo>>colTwo;
    if(rowOne==rowTwo && colOne==colTwo)
    {
        cout<<"\nEnter "<<rowOne<<"*"<<colOne<<" First Matrix Elements: ";
        for(i=0; i<rowOne; i++)
        {
            for(j=0; j<colOne; j++)
                cin>>mat1[i][j];
        }
        cout<<"Enter "<<rowOne<<"*"<<colOne<<" Second Matrix Elements: ";
        for(i=0; i<rowOne; i++)
        {
            for(j=0; j<colOne; j++)
                cin>>mat2[i][j];
        }
        cout<<"\nAddition Result of Two Given Matrix is:\n";
        for(i=0; i<rowOne; i++)
        {
            for(j=0; j<colOne; j++)
            {
                sum = mat1[i][j]+mat2[i][j];
                cout<<sum<<"  ";
            }
            cout<<endl;
        }
    }
    else
        cout<<"\nSize Mismatched!\n";
    return 0;
}

Here is its sample run supposing that user has enters the row size and column size for both the matrix is 2 and 3, and its elements for first matrix is 1, 2, 3, 4, 5, 6 and for second matrix is 6, 5, 4, 3, 2, 1:

add two matrix c++

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker