How to find the sum of the given two matrices in C++

In this program, we are going to find the sum of given two matrices in C++ with some basic commands and nested loop. This problem is based on the application of array. The logic to perform the task is almost similar to that in mathematics Following is a short and descriptive solution to perform this task.

Find the sum of given two matrices in C++

Problem statement

User enters two matrices of any size and any values. Write a program in C++ to find the sum of these two matrices (if possible).

Definition 

The sum of two matrices is possible only if the rows and column of on matrix are equal to the corresponding rows and columns of another matrix.

The sum is calculated as matrix_sum.

Problem approach

  • Initialize two arrays to act as two matrices.
  • Accept element values for them.
  • If possible, find the sum of matrices.
  • Print the result on the screen.

Source code

Following C++ program finds the sum of given matrices, is written and successfully compiled.

/* C++ program to find sum of given two matrices**
 ***give different sizes for array & the their values to get different result***
 ** enter array values in the limit -2,147,483,648 to 2,147,483,647 only **
 */

#include <iostream>
using namespace std;

int main()
{
    int m,n;
    cout<<"Enter size of first matrix in order rows and column: ";
    cin>>m>>n;                              // enter value for row and column with space

    cout<<"Enter array elements: ";
    int matrix1[m][n];
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++)
            cin>>matrix1[i][j];             // matrix 1 elements
    }

    int x,y;
    cout<<"Enter size of second matrix in order rows and column: ";
    cin>>x>>y;                              // enter value for row and column with space

    cout<<"Enter array elements: ";
    int matrix2[x][y];
    for(int i=0;i<x;i++){
        for(int j=0;j<y;j++)
            cin>>matrix2[i][j];             // matrix 2 elements
    }

    cout<<endl;
    cout<<"Matrix 1 is: "<<endl;
    for(int i=0;i<x;i++){                   // print matrix 1
        for(int j=0;j<y;j++)
            cout<<matrix1[i][j]<<"\t";
        cout<<endl;
    }

    cout<<"Matrix 2 is: "<<endl;
    for(int i=0;i<x;i++){                   // print matrix 2
        for(int j=0;j<y;j++)
            cout<<matrix2[i][j]<<"\t";
        cout<<endl;
    }

    cout<<endl;
    if(m!=x||n!=y){                         // condition for matrix sum
        cout<<"Sum of matrices is not possible!";
        return 0;
    }

    cout<<"Sum of the matrices is: "<<endl;
    for(int i=0;i<x;i++){                   // print matrix sum
        for(int j=0;j<y;j++)
            cout<<matrix1[i][j]+matrix2[i][j]<<"\t";
        cout<<endl;
    }
    return 0;
}

Example output

Enter size of first matrix in order rows and column: 2 3
Enter array elements: 56
41
23
66
23
15
Enter size of second matrix in order rows and column: 2 3
Enter array elements: 56
42
63
98
74
82

Matrix 1 is:
56 41 23
66 23 15
Matrix 2 is:
56 42 63
98 74 82

Sum of the matrices is:
112 83 86
164 97 97

Process returned 0 (0x0) execution time : 48.161 s
Press any key to continue.
Enter size of first matrix in order rows and column: 3 3
Enter array elements: 56
1
62
56
23
12
82
15
24
Enter size of second matrix in order rows and column: 2 3
Enter array elements: 45
12
85
26
25
23

Matrix 1 is:
56 1 62
56 23 12
Matrix 2 is:
45 12 85
26 25 23

Sum of matrices is not possible!
Process returned 0 (0x0) execution time : 38.641 s
Press any key to continue.

Program explanation

  1. Take input the size of matrix 1.
  2. Using these dimensions take the input of matrix elements.
  3. Repeat point 1 and 2 for matrix 2 also.
  4. Check the condition for matrix sum using if statement.
  5. If possible, use nested for loop and find the sum of the matrices.
  6. Print the result on the screen.

Also, read-

Leave a Reply

Your email address will not be published. Required fields are marked *