Skip to content

Commit bd7bfa8

Browse files
committed
fixup! feat: add exercise for pointer concept
1 parent 2a3ec53 commit bd7bfa8

File tree

7 files changed

+77
-3
lines changed

7 files changed

+77
-3
lines changed

exercises/concept/speedywagon/.docs/hints.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ int sum = (sensor_array + i)->activity; // Access activity using pointer arithme
3838
- Use the `->` operator to access the `activity` member of the `pillar_men_sensor` struct.
3939
- Think carefully about what should happen if the sensor's activity level is `0`.
4040
Should the alarm trigger?
41+
42+
## 4. Checking the data for anomalies with the `uv_alarm` function
43+
44+
- Use the `&` operator to pass a pointer to the sensor's data array into the `uv_light_heuristic` function.
45+
- Ensure you correctly check for a null pointer before accessing the sensor's data.
46+
- Compare the result of `uv_light_heuristic` with the sensor's `activity` value to determine if the alarm should trigger.

exercises/concept/speedywagon/.docs/instructions.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ In the course of this effort, you've spent years maintaining the Foundation's te
66

77
However, in recent times, the sensors that track Pillar Men activities are malfunctioning.
88
The Foundation's systems are old, and the code interacts with a legacy C++ library that cannot be updated.
9-
Your task is to implement three core functions that monitor Pillar Men sensor activity using an old-fashioned pointer-based library.
9+
Your task is to implement four core functions that monitor Pillar Men sensor activity using an old-fashioned pointer-based library.
1010

1111
The Foundation's operations rely on you.
1212

@@ -81,11 +81,25 @@ bool alarm = alarm_control(&db);
8181
// alarm => true
8282
```
8383

84+
## 4. Checking the data for anomalies with the `uv_alarm` function
85+
86+
In this task, you will implement the `uv_alarm` function to determine whether an alarm should be triggered based on UV light exposure levels and sensor activity.
87+
The `uv_alarm` function should use the provided `uv_light_heuristic` function, which operates on a vector of data and returns a value based on certain thresholds.
88+
This is a mockup version of the complex code that will run during production, please don't change the interface.
89+
90+
### Task
91+
92+
Define the `uv_alarm` function in the `speedywagon` namespace. It should:
93+
94+
- Take a pointer to a `pillar_men_sensor` _struct_ as its parameter.
95+
- Return `false` if the sensor pointer is null.
96+
- Call the `uv_light_heuristic` function, passing the address of the sensor's `data` array.
97+
- Return `true` if the value returned by `uv_light_heuristic` is greater than the `sensor->activity` level, otherwise return `false`.
98+
8499
## Wrapping Up
85100

86101
You’ve been entrusted with an essential task for the Speedywagon Foundation.
87102
By testing for valid sensor connections, counting activity, and implementing alarm controls, you’ve ensured that the Foundation's battle against the Pillar Men can continue uninterrupted.
88103

89104
As a modern C++ engineer, you’d prefer using smart pointers, but alas, legacy code demands respect for the old ways.
90105
The fate of humanity may rest on these pointers, so proceed carefully, and may the Hamon energy guide you.
91-

exercises/concept/speedywagon/.meta/exemplar.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
namespace speedywagon {
44

5+
int uv_light_heuristic(std::vector<int>* data_array) {
6+
double avg{};
7+
for (auto element : *data_array) {
8+
avg += element;
9+
}
10+
avg /= data_array->size();
11+
int uv_index{};
12+
for (auto element : *data_array) {
13+
if (element > avg) ++uv_index;
14+
}
15+
return uv_index;
16+
}
17+
518
bool connection_check(pillar_men_sensor* sensor) { return sensor != nullptr; }
619

720
int activity_counter(pillar_men_sensor* sensor_array, int capacity) {
@@ -17,4 +30,9 @@ bool alarm_control(pillar_men_sensor* sensor) {
1730
return sensor->activity > 0;
1831
}
1932

33+
bool uv_alarm(pillar_men_sensor* sensor) {
34+
if (sensor == nullptr) return false;
35+
return uv_light_heuristic(&(sensor->data)) > sensor->activity;
36+
}
37+
2038
} // namespace speedywagon

exercises/concept/speedywagon/.meta/exemplar.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ struct pillar_men_sensor {
1111
std::vector<int> data{};
1212
};
1313

14+
int uv_light_heuristic(std::vector<int>* data_array);
15+
1416
bool connection_check(pillar_men_sensor* sensor);
1517

1618
int activity_counter(pillar_men_sensor* sensor_array, int capacity);
1719

1820
bool alarm_control(pillar_men_sensor* sensor);
1921

22+
bool uv_alarm(pillar_men_sensor* sensor);
23+
2024
} // namespace speedywagon
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
#include "speedywagon.h"
22

3-
namespace speedywagon {} // namespace speedywagon
3+
namespace speedywagon {
4+
5+
// Enter your code below:
6+
7+
// Please don't change the interface of the uv_light_heuristic function
8+
int uv_light_heuristic(std::vector<int>* data_array) {
9+
double avg{};
10+
for (auto element : *data_array) {
11+
avg += element;
12+
}
13+
avg /= data_array->size();
14+
int uv_index{};
15+
for (auto element : *data_array) {
16+
if (element > avg) ++uv_index;
17+
}
18+
return uv_index;
19+
}
20+
21+
} // namespace speedywagon

exercises/concept/speedywagon/speedywagon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ struct pillar_men_sensor {
1111
std::vector<int> data{};
1212
};
1313

14+
int uv_light_heuristic(std::vector<int>* data_array);
15+
1416
} // namespace speedywagon

exercises/concept/speedywagon/speedywagon_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,16 @@ TEST_CASE("alarm_control: works correctly for nullptr", "[task_3]") {
4646
REQUIRE(speedywagon::alarm_control(&santana));
4747
}
4848

49+
TEST_CASE("uv_alarm: works correctly for nullptr", "[task_4]") {
50+
speedywagon::pillar_men_sensor* wham{nullptr};
51+
REQUIRE_FALSE(speedywagon::uv_alarm(wham));
52+
}
53+
54+
TEST_CASE("uv_alarm: works correctly with mock data", "[task_4]") {
55+
speedywagon::pillar_men_sensor wham{0, "Rome", {1, 605, 313, 4000}};
56+
REQUIRE(speedywagon::uv_alarm(&wham));
57+
wham.activity = 9001;
58+
REQUIRE_FALSE(speedywagon::uv_alarm(&wham));
59+
}
60+
4961
#endif

0 commit comments

Comments
 (0)