-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAddTwoMatrices.cpp
54 lines (42 loc) · 1.14 KB
/
AddTwoMatrices.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
50
51
52
53
54
#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 << "\nEnter elements of First matrix:\n";
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
cout << "Enter elements of Second matrix:\n";
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "Enter element a" << 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 result
cout << "\nSum of two matrix is: \n\n";
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "\t" << sum[i][j];
if (j == c - 1)
{
cout << "\n\n";
}
}
return 0;
}