Add Two Matrices using Multi-dimensional Arrays.Today we make a simple C++ Program To add two matrices in C++ Programming, you have to ask to the user to enter the elements of both the matrix, now start adding the two matrix to form a new matrix.
C++ Program to Add Two Matrix Using Multi-dimensional Arrays
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the Number of Rows and Columns of Matrix:";
cin >> m >> n;
cout << "Enter the First Elements of Matrix:\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "Enter the Second elements of matrix:\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of Entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
return 0;
}
Output of Program
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