-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverageOfNums.cpp
More file actions
52 lines (47 loc) · 1.19 KB
/
averageOfNums.cpp
File metadata and controls
52 lines (47 loc) · 1.19 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
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
void average(vector<vector<int>>& A, vector<vector<int>>& B, int n, int r1, int r2, int c1, int c2) {
for (int i = r1; i <= r2; i++) {
for (int j = c1; j <= c2; j++) {
int N, E, S, W;
N = (i - 1 >= 0)? A[i - 1][j] : 0;
E = (j + 1 <= n - 1) ? A[i][j + 1] : 0;
S = (i + 1 <= n - 1) ? A[i + 1][j] : 0;
W = (j - 1 >= 0) ? A[i][j - 1] : 0;
B[i][j] = (N + E + S + W) / 4;
}
}
}
int main() {
int n;
cout << "Enter matrix size" << endl;
cin >> n;
vector<vector<int>> A(n, vector<int>(n));
for (auto &i : A) {
for (auto &j : i) { j = rand() % 5; }
}
vector<vector<int>> B(n, vector<int>(n));
thread t1{ average, ref(A), ref(B), n, 0, n / 2, 0, n / 2 }; //Using {} is more recent
thread t2{ average, ref(A), ref(B), n, 0, n / 2, n / 2+1, n-1 };
thread t3{ average, ref(A), ref(B), n, n/2+1, n-1, 0, n / 2 };
thread t4{ average, ref(A), ref(B), n, n/2+1, n-1, n/2+1, n-1 };
t1.join();
t2.join();
t3.join();
t4.join();
for (auto i : A) {
for (auto j : i) {
cout << j << " ";
}
cout << endl;
}
cout << endl;
for (auto i : B) {
for (auto j : i) {
cout << j << " ";
}
cout << endl;
}
}