Skip to content

Commit eb6fad5

Browse files
Operator Overloading Problem
1 parent 0ad806f commit eb6fad5

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Link - https://www.hackerrank.com/challenges/operator-overloading/problem?isFullScreen=true
2+
3+
#include <cmath>
4+
#include <cstdio>
5+
#include <vector>
6+
#include <iostream>
7+
#include <algorithm>
8+
using namespace std;
9+
10+
class Matrix {
11+
12+
public:
13+
Matrix() {}
14+
Matrix(const Matrix& x) : a(x.a) {}
15+
Matrix(const vector<vector<int>>& v) : a(v) {}
16+
Matrix operator+(const Matrix&);
17+
vector<vector<int>> a;
18+
};
19+
20+
Matrix Matrix::operator+(const Matrix& m){
21+
vector<vector<int>> vv = a;
22+
for (int i=0; i<vv.size(); i++){
23+
for (int j=0; j<vv[0].size(); j++){
24+
vv[i][j] += m.a[i][j];
25+
}
26+
}
27+
return Matrix(vv);
28+
}
29+
30+
31+
int main () {
32+
int cases,k;
33+
cin >> cases;
34+
for(k=0;k<cases;k++) {
35+
Matrix x;
36+
Matrix y;
37+
Matrix result;
38+
int n,m,i,j;
39+
cin >> n >> m;
40+
for(i=0;i<n;i++) {
41+
vector<int> b;
42+
int num;
43+
for(j=0;j<m;j++) {
44+
cin >> num;
45+
b.push_back(num);
46+
}
47+
x.a.push_back(b);
48+
}
49+
for(i=0;i<n;i++) {
50+
vector<int> b;
51+
int num;
52+
for(j=0;j<m;j++) {
53+
cin >> num;
54+
b.push_back(num);
55+
}
56+
y.a.push_back(b);
57+
}
58+
result = x+y;
59+
for(i=0;i<n;i++) {
60+
for(j=0;j<m;j++) {
61+
cout << result.a[i][j] << " ";
62+
}
63+
cout << endl;
64+
}
65+
}
66+
return 0;
67+
}

0 commit comments

Comments
 (0)