#include <iostream>
class ADCSensor {
private:
// Fixed calibration offset hidden from the user
const int offset_val;
public:
// Initialize offset to 100
ADCSensor() : offset_val(100) {}
// Process raw data into calibrated application data
int getCalibratedSample(int raw) const {
// Apply offset
int calibrated = raw - offset_val;
// Saturate to 0 (Clamp negative results)
// This handles cases where noise drops the reading below the offset
if (calibrated < 0) {
return 0;
}
return calibrated;
}
};
int main() {
int N;
if (!(std::cin >> N)) return 0;
ADCSensor sensor;
for (int i = 0; i < N; ++i) {
int raw;
std::cin >> raw;
// Use the encapsulated interface to get clean data
std::cout << "Sample: " << sensor.getCalibratedSample(raw) << std::endl;
}
return 0;
}Explanation & Logic Summary:
This problem addresses Data Transformation and Validity.
raw - offset inside main(). This exposes the offset value to the user and fails to handle the edge case where noise pulls the reading below the offset (resulting in negative numbers).getCalibratedSample method encapsulates the correction algorithm. It centralizes the subtraction and adds the critical check if (calibrated < 0) return 0;.Firmware Relevance & Real-World Context:
main leads to inconsistent data.offset_val) are often stored in OTP (One-Time Programmable) memory or Flash and should be const or private to prevent accidental modification during runtime.
Input
3 250 100 50
Expected Output
Sample: 150 Sample: 0 Sample: 0