#include <iostream>
using namespace std;
// Inline function to clamp a value between minVal and maxVal
inline int saturate(int value, int minVal, int maxVal) {
if (value < minVal) return minVal;
if (value > maxVal) return maxVal;
return value;
}
int main() {
int value, minVal, maxVal;
cin >> value >> minVal >> maxVal;
cout << saturate(value, minVal, maxVal);
return 0;
}
Explanation & Logic Summary:
Inline functions reduce call overhead for small, frequently used operations like saturation.
Saturation is widely used in embedded firmware to ensure values remain within safe and valid limits, such as:
The function enforces bounds safely and efficiently.
Firmware Relevance & Real-World Context:
Saturation prevents invalid or unsafe values from propagating due to sensor noise, arithmetic errors, or control-loop instability.
This pattern is common in:
Input
120 0 100
Expected Output
100