Initialize Filter3 Class

#include <iostream>
using namespace std;

class Filter3 {
private:
    const int id;      // Constant member: must be initialized in the list
    int coeff[3];      // Fixed-size array
    int lastOutput;    // State variable

public:
    /**
     * Constructor using an Initialization List.
     * This is more efficient than assignment and required for 'const' members.
     */
    Filter3(int filterId, int c0, int c1, int c2) 
        : id(filterId), 
          coeff{c0, c1, c2}, 
          lastOutput(0) 
    {
        // Body is empty as per requirements!
    }

    /**
     * apply: Updates the internal state based on the input x.
     * Formula: x * coeff[0] + coeff[1] + coeff[2]
     */
    void apply(int x) {
        lastOutput = (x * coeff[0]) + coeff[1] + coeff[2];
    }

    /**
     * read: Returns the most recent calculation.
     */
    int read() const {
        return lastOutput;
    }
};

int main() {
    int id, c0, c1, c2;
    if (!(cin >> id >> c0 >> c1 >> c2)) return 0;

    // Instantiate the class using the provided inputs
    Filter3 f(id, c0, c1, c2);

    int s1, s2;
    if (!(cin >> s1 >> s2)) return 0;

    // Process the samples
    f.apply(s1);
    f.apply(s2);

    // Output the final result
    cout << f.read() << endl;

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

7 2 3 1 10 4

Expected Output

12