67. SensorConfig Initialization

Create a class SensorConfig that represents configuration data for a digital sensor in an embedded system.

Each sensor must be initialized with valid configuration data at startup, specifically a sensor ID and a calibration offset. The system must not allow uninitialized sensor objects.

The class must store the latest calibrated sensor value and update it whenever new raw data is received.

Class Requirements

The class SensorConfig must contain the following private data members:

  • int id — unique sensor identifier
  • int offset — calibration offset applied to raw readings
  • int lastValue — last calibrated value (must be initialized to 0)

Constructor Requirements

The class must define only one constructor:

SensorConfig(int sensorId, int calibrationOffset)

The constructor must:

  • Assign sensorId to id
  • Assign calibrationOffset to offset
  • Initialize lastValue to 0

❗ A default constructor must not be used.

Public Member Functions

  • void update(int raw)
    • Applies calibration using the formula:
      • lastValue = raw + offset
        
  • int read()
    • Returns the current value of lastValue

Main Function Behavior

In main():

  1. Read two integers: id and offset
  2. Create a SensorConfig object using the parameterized constructor
  3. Read two raw sensor values: r1 and r2
  4. Call update(r1) followed by update(r2)
  5. Print the final calibrated value returned by read()

Input Format

id offset
r1 r2
  • All values are integers
  • Offsets may be negative

Output Format

<final_calibrated_value>
  • Output only the final calibrated value
  • No extra spaces or newlines

 

Example Input

10 3
20 25

Example Output

28

Explanation

  • Offset = 3
  • update(20)lastValue = 23
  • update(25)lastValue = 28
  • Final output is 28

 

Constraints

  • Only the parameterized constructor must be used
  • Calibration offset must be applied on every update
  • Output formatting must match exactly
  • Use standard integer arithmetic

 

 

 

 

Loading...

Input

10 3 20 25

Expected Output

28