-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem2.cpp
More file actions
187 lines (156 loc) · 4.66 KB
/
problem2.cpp
File metadata and controls
187 lines (156 loc) · 4.66 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// David Huynh
// COP 4520, Spring 2024
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <shared_mutex>
#include <random>
#include <algorithm>
#define THREADS 8
#define TIMEINMILLISECONDS 50
class TemperatureReadingModule
{
private:
std::shared_mutex mtx;
std::vector<int> data;
int timer;
bool scanned;
public:
TemperatureReadingModule() : timer(0), scanned(true) {}
// Generates a random temperature for the scan.
void scanTemperature(int tNum)
{
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(-100, 70);
std::unique_lock lock(mtx);
int n = dist(mt);
std::cout << "> Sensor " << tNum << " got " << n << "F.\n";
data.push_back(n);
scanned = true;
}
// Checks if a thread is allowed to take a temperature scan.
bool scanAvailable(int n)
{
std::shared_lock lock(mtx);
// Allows only 1 scan from a specific thread.
if (scanned == false && timer <= 60 && timer % 8 == n)
{
return true;
}
return false;
}
// Allows the main thread to act as a timer. Everytime it increments, a new scan is allowed.
void incrementTimer()
{
std::unique_lock lock(mtx);
timer++;
scanned = false;
std::cout << "Minute " << timer << "\n";
}
// Allows the threads to check the timer.
int getTimer()
{
std::shared_lock lock(mtx);
return timer;
}
// Prints the report after the temperature readings.
void compileReport()
{
std::cout << "\n******\n";
std::cout << "REPORT\n";
std::cout << "******\n";
std::vector<int> sortedData = data;
std::sort(sortedData.begin(), sortedData.end());
std::cout << "Top 5 Highest Temperatures:\n";
int j = sortedData.size() - 1;
for (auto i = 0; i < 5; i++)
{
std::cout << sortedData[j--] << "F\n";
}
std::cout << "\nTop 5 Lowest Temperatures:\n";
for (int i = 0; i < 5; i++)
{
std::cout << sortedData[i] << "F\n";
}
std::vector<int> interval;
int maxDiff = 0;
int lowMin = 0;
int high = 0;
int low = 0;
// Check each interval for the largest difference.
for (int i = 0; i < 51; i++)
{
std::vector<int> unsorted;
for (int j = i; j < i+10; j++)
{
unsorted.push_back(data[j]);
}
std::vector<int> sorted = unsorted;
std::sort(sorted.begin(), sorted.end());
int n = sorted[9] - sorted[0];
if (n > maxDiff)
{
maxDiff = n;
lowMin = i;
interval = unsorted;
high = sorted[9];
low = sorted[0];
}
}
std::cout << "\n10-minute interval of largest temperature difference (inclusive):\n";
std::cout << "> Minute " << lowMin + 1 << " to " << lowMin + 10 << "\n";
std::cout << "> Highest: " << high << "F\n";
std::cout << "> Lowest: " << low << "F\n";
std::cout << "> Difference: " << high - low << "F\n";
std::cout << "\nInterval:\n";
for (auto i : interval)
{
std::cout << i << "F ";
}
std::cout << "\n";
}
};
// Allows each scanner to take a temperature reading.
void temperatureSensor(int tNum, TemperatureReadingModule &t)
{
// Loop until we go over an hour.
while (t.getTimer() <= 60)
{
// Each thread checks if it is allowed to scan.
if (t.scanAvailable(tNum))
{
t.scanTemperature(tNum);
}
}
}
// Main function that spawns 8 threads (sensors).
int main(void)
{
TemperatureReadingModule t;
std::thread t1(temperatureSensor, 0, std::ref(t));
std::thread t2(temperatureSensor, 1, std::ref(t));
std::thread t3(temperatureSensor, 2, std::ref(t));
std::thread t4(temperatureSensor, 3, std::ref(t));
std::thread t5(temperatureSensor, 4, std::ref(t));
std::thread t6(temperatureSensor, 5, std::ref(t));
std::thread t7(temperatureSensor, 6, std::ref(t));
std::thread t8(temperatureSensor, 7, std::ref(t));
// Main thread acts as a timer for the other threads.
for (int i = 0; i <= 60; i++)
{
t.incrementTimer();
std::this_thread::sleep_for (std::chrono::milliseconds(TIMEINMILLISECONDS));
}
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
t7.join();
t8.join();
t.compileReport();
return 0;
}