|
1 | 1 | # Instructions |
2 | 2 |
|
3 | | -In this exercise, you will be simulating a windowing based computer system. |
4 | | -You will create some windows that can be moved and resized. |
5 | | -The following image is representative of the values you will be working with below. |
| 3 | +Welcome, Engineer! |
| 4 | +You are one of the last veterans of the Speedywagon Foundation, a secret organization that, for decades, has been battling ancient threats like the Pillar Men. |
| 5 | +In the course of this effort, you've spent years maintaining the Foundation's technological systems, built using a mix of cutting-edge tech and aging libraries. |
6 | 6 |
|
7 | | -``` |
8 | | - <--------------------- screenSize.width ---------------------> |
9 | | -
|
10 | | - ^ ╔════════════════════════════════════════════════════════════╗ |
11 | | - | ║ ║ |
12 | | - | ║ position.x,_ ║ |
13 | | - | ║ position.y \ ║ |
14 | | - | ║ \<----- size.width -----> ║ |
15 | | - | ║ ^ *──────────────────────┐ ║ |
16 | | - | ║ | │ title │ ║ |
17 | | - | ║ | ├──────────────────────┤ ║ |
18 | | -screenSize.height ║ | │ │ ║ |
19 | | - | ║ size.height │ │ ║ |
20 | | - | ║ | │ contents │ ║ |
21 | | - | ║ | │ │ ║ |
22 | | - | ║ | │ │ ║ |
23 | | - | ║ v └──────────────────────┘ ║ |
24 | | - | ║ ║ |
25 | | - | ║ ║ |
26 | | - v ╚════════════════════════════════════════════════════════════╝ |
27 | | -``` |
| 7 | +However, in recent times, the sensors that track Pillar Men activities are malfunctioning. |
| 8 | +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. |
28 | 10 |
|
29 | | -## 1. Define a `size` struct for storing the dimensions of the window |
| 11 | +The Foundation's operations rely on you. |
30 | 12 |
|
31 | | -Define a `struct` named `size`. |
32 | | -It should have two member variables `width` and `height` that store the window's current dimensions. |
33 | | -The constructor function should accept initial values for these fields. |
34 | | -The width is provided as the first parameter, the height as the second one. |
35 | | -The default width and height should be `80` and `60`, respectively. |
| 13 | +## 0. The Sensor Environment (`pillar_men_sensor`) |
36 | 14 |
|
37 | | -Additionally, define a member function `void resize(int newWidth, int newHeight)` that takes a new width and height as parameters and changes the fields to reflect the new size. |
| 15 | +As sensor readings can be huge, we supply a mockup _struct_ that is used in the actual library. |
| 16 | +The code has already been implemented in the header file for you. |
38 | 17 |
|
39 | 18 | ```cpp |
40 | | -size aSize{1080, 764}; |
41 | | -aSize.width; |
42 | | -// => 1080 |
43 | | -aSize.height; |
44 | | -// => 764 |
45 | | - |
46 | | -aSize.resize(1920, 1080); |
47 | | -aSize.width; |
48 | | -// => 1920 |
49 | | -aSize.height; |
50 | | -// => 1080 |
| 19 | +struct pillar_men_sensor { |
| 20 | + int activity{}; |
| 21 | + std::string location{}; |
| 22 | + std::vector<int> data{}; |
| 23 | +}; |
51 | 24 | ``` |
52 | 25 |
|
53 | | -## 2. Define a `position` struct to store a window position |
54 | | -
|
55 | | -Define a struct (or class) named `position` with two member variables, `x` and `y` that store the current horizontal and vertical position, respectively, of the window's upper left corner. |
56 | | -The constructor function should accept initial values for these fields. |
57 | | -The value for `x` is provided as the first parameter, the value for `y` as the second one. |
58 | | -The default value should be `0` for both variables. |
59 | | -
|
60 | | -The position (0, 0) is the upper left corner of the screen with `x` values getting larger as you move right and `y` values getting larger as you move down. |
| 26 | +## 1. Check Sensor Connection (`connection_check`) |
61 | 27 |
|
62 | | -Also define a method `void move(int newX, int newY)` that takes new x and y parameters and changes the properties to reflect the new position. |
63 | | -
|
64 | | -```cpp |
65 | | -position point{}; |
66 | | -point.x; |
67 | | -// => 0 |
68 | | -point.y; |
69 | | -// => 0 |
70 | | -
|
71 | | -point.move(100, 200); |
72 | | -point.x; |
73 | | -// => 100 |
74 | | -point.y; |
75 | | -// => 200 |
76 | | -``` |
| 28 | +Your first task is to ensure that the Pillar Men sensor is connected properly. |
| 29 | +We can't have false alarms triggered by disconnected sensors. |
| 30 | +You will write a function `connection_check`, which tests if the sensor's pointer is valid by checking for `nullptr`. |
77 | 31 |
|
78 | | -## 3. Define a `programWindow` class |
| 32 | +### Task |
79 | 33 |
|
80 | | -Define a `programWindow` class or struct with __pointers__ to the following member variables: |
| 34 | +- Define a function that accepts a pointer a a `pillar_men_sensor` _struct_. |
| 35 | +- The function should return `true` if the sensor pointer is not null, and `false` otherwise. |
81 | 36 |
|
82 | | -- `screenSize`: holds a pointer of type `size` with `width` 800 and `height` 600 |
83 | | -- `windowSize` : holds a pointer of type `size`, the initial value is the default value of the `size` instance |
84 | | -- `windowPosition` : holds a pointer of type `position`, the initial value is the default value of the `position` instance |
85 | | - |
86 | | -When the window is opened (initialized), it always has the default `windowSize` and `windowPosition` in the beginning. |
87 | | -Its constructor should not take any input parameters. |
| 37 | +### Example |
88 | 38 |
|
89 | 39 | ```cpp |
90 | | -programWindow aProgramWindow{}; |
91 | | -programWindow.screenSize->width; |
92 | | -// => 800 |
93 | | - |
94 | | -// Similar for the other fields. |
| 40 | +pillar_men_sensor* sensor{nullptr}; |
| 41 | +bool isConnected = connection_check(sensor); |
| 42 | +// isConnected => false |
95 | 43 | ``` |
96 | 44 |
|
97 | | -## 4. Add a method to resize the window |
| 45 | +## 2. Count Activity of Sensors (`activity_counter`) |
| 46 | + |
| 47 | +Pillar Men are lurking in the shadows, and we need to know if sensors have detected any activity. |
| 48 | +You will write the `activity_counter` function, which takes in an array of sensors and a capacity indicating the number of sensors in the array. |
98 | 49 |
|
99 | | -The `programWindow` class should include a function `resize`. |
100 | | -It should accept a __pointer__ of type `size` as input and attempts to resize the window to the specified size. |
| 50 | +### Task |
101 | 51 |
|
102 | | -However, the new size cannot exceed certain bounds. |
| 52 | +- Define a function that accepts a pointer to the first element of an array and the arrays capacity. |
| 53 | +- Use pointer arithmetic to loop through the sensor array and accumulate the activity readings. |
| 54 | +- Return the accumulated activity. |
103 | 55 |
|
104 | | -- The minimum allowed height or width is 1. |
105 | | - Requested heights or widths less than 1 will be clipped to 1. |
106 | | -- The maximum height and width depend on the current position of the window, the edges of the window cannot move past the edges of the screen. |
107 | | - Values larger than these bounds will be clipped to the largest size they can take. |
108 | | - E.g. if the window's position is at `x` = 400, `y` = 300 and a resize to `height` = 400, `width` = 300 is requested, then the window would be resized to `height` = 300, `width` = 300 as the screen is not large enough in the `y` direction to fully accommodate the request. |
| 56 | +### Example |
109 | 57 |
|
110 | 58 | ```cpp |
111 | | -programWindow aProgramWindow{}; |
112 | | - |
113 | | -size newSize{600, 400}; |
114 | | -aProgramWindow.resize(&newSize); |
115 | | -aProgramWindow.size->width; |
116 | | -// => 600 |
117 | | -aProgramWindow.size->height; |
118 | | -// => 400 |
| 59 | +pillar_men_sensor sensor_array[3] = {{0}, {101}, {22}}; |
| 60 | +int totalActivity = activity_counter(sensor_array, 3); |
| 61 | +// totalActivity => 123 |
119 | 62 | ``` |
120 | 63 |
|
121 | | -## 5. Add a method to move the window |
| 64 | +## 3. Alarm Control (`alarm_control`) |
122 | 65 |
|
123 | | -Besides the resize functionality, the `programWindow` class should also include a function `move`. |
124 | | -It should accept a parameter as a __pointer__ of type `position` as input. |
125 | | -The `move` function is similar to `resize` however, this function adjusts the _position_ of the window to the requested value, rather than the size. |
| 66 | +Not every sensor should trigger an alarm unless there’s real danger. |
| 67 | +The `alarm_control` function ensures that a sensor only triggers an alarm if its activity level is greater than 0. |
| 68 | +This function should also check for null sensors to prevent system crashes. |
126 | 69 |
|
127 | | -As with `resize` the new position cannot exceed certain limits. |
| 70 | +### Task |
128 | 71 |
|
129 | | -- The smallest position is 0 for both `x` and `y`. |
130 | | -- The maximum position in either direction depends on the current size of the window. |
131 | | - The edges cannot move past the edges of the screen. |
132 | | - Values larger than these bounds will be clipped to the largest size they can take. |
133 | | - E.g. if the window's size is at `x` = 250, `y` = 100 and a move to `x` = 600, `y` = 200 is requested, then the window would be moved to `x` = 550, `y` = 200 as the screen is not large enough in the `x` direction to fully accommodate the request. |
| 72 | +- Define a function that accepts the pointer to a `pillar_men_sensor`. |
| 73 | +- The function should first check for a `nullptr` sensor. If the sensor is `nullptr`, return `false`. |
| 74 | +- If the sensor is valid and its activity is greater than 0, return `true`; otherwise, return `false`. |
| 75 | +
|
| 76 | +### Example |
134 | 77 |
|
135 | 78 | ```cpp |
136 | | -programWindow aProgramWindow{}; |
137 | | -
|
138 | | -position newPosition{50, 100}; |
139 | | -aProgramWindow.move(&newPosition); |
140 | | -aProgramWindow.position->x; |
141 | | -// => 50 |
142 | | -aProgramWindow.position->y; |
143 | | -// => 100 |
| 79 | +pillar_men_sensor db{9008, "songokunoie", {7, 7, 7}}; |
| 80 | +bool alarm = alarm_control(&db); |
| 81 | +// alarm => true |
144 | 82 | ``` |
145 | 83 |
|
146 | | -## 6. Change a program window |
147 | | - |
148 | | -Implement a `changeWindow` function that accepts a __pointer__ to a `programWindow` object as input and changes the window to the specified size and position. |
| 84 | +## Wrapping Up |
149 | 85 |
|
150 | | -The window should get a width of 400, a height of 300 and be positioned at x = 100, y = 150. |
| 86 | +You’ve been entrusted with an essential task for the Speedywagon Foundation. |
| 87 | +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. |
151 | 88 |
|
152 | | -```cpp |
153 | | -programWindow aProgramWindow{}; |
154 | | -changeWindow(&programWindow); |
155 | | -aProgramWindow.size->width; |
156 | | -// => 400 |
| 89 | +As a modern C++ engineer, you’d prefer using smart pointers, but alas, legacy code demands respect for the old ways. |
| 90 | +The fate of humanity may rest on these pointers, so proceed carefully, and may the Hamon energy guide you. |
157 | 91 |
|
158 | | -// Similar for the other fields. |
159 | | -``` |
0 commit comments