-
Notifications
You must be signed in to change notification settings - Fork 1
/
Add Two Matrices using Multi-dimensional Arrays.cpp
49 lines (40 loc) · 1.48 KB
/
Add Two Matrices using Multi-dimensional Arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
int main()
{
int row, colomn, a[100][100], b[100][100], sum[100][100];
cout << "Enter number of rows between 1 and 100 : ";
cin >> row;
cout << "Enter number of columns between 1 and 100 : ";
cin >> colomn;
cout << endl << "Enter elements of 1st matrix : " << endl;
// Storing elements of first matrix entered by user.
for(int i = 0; i < row; ++i)
for(int j = 0; j < colomn; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
// Storing elements of second matrix entered by user.
cout << endl << "\nEnter elements of 2nd matrix: " << endl;
for(int i = 0; i < row; ++i)
for(int j = 0; j < colomn; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Adding Two matrices
for(int i = 0; i < row; ++i)
for(int j = 0; j < colomn; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(int i = 0; i < row; ++i)
for(int j = 0; j < colomn; ++j)
{
cout << sum[i][j] << " ";
if(j == colomn - 1)
cout << endl;
}
return 0;
}