15. Sensor Filter Selection

#include <iostream>
#include <string>
using namespace std;

// Define available filter modes
enum FilterMode {
    NONE,
    LOW,
    HIGH
};

// Default filter mode is NONE
int readSensor(int raw, FilterMode mode = NONE) {
    if (mode == NONE) {
        return raw;
    }
    if (mode == LOW) {
        return raw / 2;
    }
    return raw / 4; // HIGH filter
}

int main() {
    int raw, modeFlag;
    cin >> raw >> modeFlag;

    if (modeFlag == 0) {
        cout << readSensor(raw);
    } else {
        string filterName;
        cin >> filterName;

        FilterMode filter = (filterName == "LOW") ? LOW : HIGH;
        cout << readSensor(raw, filter);
    }

    return 0;
}

Explanation & Logic Summary

  • The sensor-reading function uses a default argument, allowing it to be called with or without a filter mode.
  • If no filter is provided, NONE is automatically selected.
  • An enum is used to represent filter modes, improving readability and type safety.
  • LOW and HIGH modes apply progressively stronger integer-based smoothing.
     

Firmware Relevance & Real-World Context

  • Embedded sensor drivers often provide optional filtering or averaging modes.
  • Default arguments simplify common API usage while allowing configurability.
  • Enums prevent invalid configurations and improve code clarity.

 

 

 


 

Loading...

Input

100 0

Expected Output

100