Sensor Calibration Class

#include <iostream>

class Sensor {
private:
    int rawValue; // Stores the most recent raw sensor reading.
        int offset;   // Stores the calibration offset.

        public:
            /**
                 * @brief Stores a new raw sensor reading.
                      * @param value The new raw sensor value.
                           */
                               void setRaw(int value) {
                                       rawValue = value;
                                           }

                                               /**
                                                    * @brief Sets the calibration offset.
                                                         * @param off The calibration offset.
                                                              */
                                                                  void calibrate(int off) {
                                                                          offset = off;
                                                                              }

                                                                                  /**
                                                                                       * @brief Returns the calibrated sensor value.
                                                                                            * @return The calibrated value (rawValue + offset).
                                                                                                 */
                                                                                                     int read() const {
                                                                                                             return rawValue + offset;
                                                                                                                 }
                                                                                                                 };

                                                                                                                 int main() {
                                                                                                                     int initialRaw;
                                                                                                                         int calibrationOffset;
                                                                                                                             int newRaw;

                                                                                                                                 // Read the three required integers from standard input
                                                                                                                                     if (!(std::cin >> initialRaw >> calibrationOffset >> newRaw)) {
                                                                                                                                             // Handle potential input error (optional, per constraints input is guaranteed)
                                                                                                                                                     return 1;
                                                                                                                                                         }

                                                                                                                                                             // Create a Sensor object
                                                                                                                                                                 Sensor mySensor;

                                                                                                                                                                     // Set the initial raw value
                                                                                                                                                                         mySensor.setRaw(initialRaw);

                                                                                                                                                                             // Set the calibration offset
                                                                                                                                                                                 mySensor.calibrate(calibrationOffset);

                                                                                                                                                                                     // Set the new raw value, overwriting the initial one
                                                                                                                                                                                         mySensor.setRaw(newRaw);

                                                                                                                                                                                             // Print the final calibrated sensor reading
                                                                                                                                                                                                 std::cout << mySensor.read() << std::endl;

                                                                                                                                                                                                     return 0;
                                                                                                                                                                                                     }
                                                                                                                                                                                                     

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

100 -5 120

Expected Output

115