SensorConfig Initialization

#include <iostream>
using namespace std;

class SensorConfig {
private:
    int id;         // Unique sensor identifier
    int offset;     // Calibration offset applied to raw readings
    int lastValue;  // The most recent calibrated value

public:
    // Parameterized constructor: Initializes the sensor with ID and offset.
    // lastValue is explicitly initialized to 0 as per requirements.
    SensorConfig(int sensorId, int calibrationOffset) {
        id = sensorId;
        offset = calibrationOffset;
        lastValue = 0;
    }

    // Applies the calibration formula to the raw input and stores it.
    void update(int raw) {
        lastValue = raw + offset;
    }

    // Returns the current calibrated value.
    int read() {
        return lastValue;
    }
};

int main() {
    int id, offset;
    if (!(cin >> id >> offset)) return 0;

    // Create the object using the parameterized constructor
    SensorConfig cfg(id, offset); 

    int r1, r2;
    if (!(cin >> r1 >> r2)) return 0;

    // Update the sensor with two consecutive raw readings
    cfg.update(r1);
    cfg.update(r2);

    // Output only the final value
    cout << cfg.read();
    
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

10 3 20 25

Expected Output

28