-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathoperator-overloading.cpp
More file actions
79 lines (74 loc) · 1.75 KB
/
operator-overloading.cpp
File metadata and controls
79 lines (74 loc) · 1.75 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Operator Overloading
// Learn how to overload operators. Print the sum of two matrices and print the resultant matrix.
//
// https://www.hackerrank.com/challenges/operator-overloading/problem
//
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// (skeliton_head) ----------------------------------------------------------------------
class Matrix
{
public:
vector<vector<int>> a;
Matrix& operator+(const Matrix& r)
{
if (a.size() == r.a.size())
{
for (size_t i = 0; i < a.size(); ++i)
{
auto& ai = a[i];
const auto& ri = r.a[i];
if (ai.size() == ri.size())
{
for (size_t j = 0; j < ai.size(); ++j)
{
ai[j] += ri[j];
}
}
}
}
return *this;
}
};
// (skeliton_tail) ----------------------------------------------------------------------
int main () {
int cases,k;
cin >> cases;
for(k=0;k<cases;k++) {
Matrix x;
Matrix y;
Matrix result;
int n,m,i,j;
cin >> n >> m;
for(i=0;i<n;i++) {
vector<int> b;
int num;
for(j=0;j<m;j++) {
cin >> num;
b.push_back(num);
}
x.a.push_back(b);
}
for(i=0;i<n;i++) {
vector<int> b;
int num;
for(j=0;j<m;j++) {
cin >> num;
b.push_back(num);
}
y.a.push_back(b);
}
result = x+y;
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
cout << result.a[i][j] << " ";
}
cout << endl;
}
}
return 0;
}