68. Initialize Filter3 Class

#include <iostream>
using namespace std;

class Filter3 {
private:
    const int id;
    int coeff[3];
    int lastOutput;

public:
    Filter3(int filterId, int c0, int c1, int c2)
        : id(filterId), coeff{c0, c1, c2}, lastOutput(0) {}

    void apply(int x) {
        lastOutput = x * coeff[0] + coeff[1] + coeff[2];
    }

    int read() {
        return lastOutput;
    }
};

int main() {
    int id, c0, c1, c2;
    cin >> id >> c0 >> c1 >> c2;

    Filter3 f(id, c0, c1, c2);

    int s1, s2;
    cin >> s1 >> s2;

    f.apply(s1);
    f.apply(s2);

    cout << f.read();
    return 0;
}

Explanation & Logic Summary

  • Constructor initialization lists are required for:
    • const members
    • Fixed-size arrays
  • Initialization lists guarantee deterministic startup behavior in firmware
  • The filter applies a simple linear computation using fixed coefficients
  • Only the most recent output is stored

Firmware Relevance & Real-World Context

  • Fixed-coefficient filters are common in MCU signal-processing pipelines
  • Initialization lists mirror hardware register and peripheral setup
  • const identifiers model immutable hardware or channel IDs
  • This pattern is widely used in sensor processing, DSP, and control firmware

 

 

 

 

Loading...

Input

7 2 3 1 10 4

Expected Output

12