68. Initialize Filter3 Class

Create a class Filter3 that represents a simple 3-coefficient digital filter used in embedded signal processing.

This problem focuses on correct use of constructor initialization lists in Embedded C++, which are mandatory when initializing const members and fixed-size arrays.

All internal members must be initialized using a constructor initialization list, not by assignment inside the constructor body.

Class Requirements

  • Private Members
    • const int id
    • int coeff[3]
    • int lastOutput
  • Constructor Requirements
    • Create the following constructor:
      • Filter3(int filterId, int c0, int c1, int c2)
    • The constructor must initialize all members using an initialization list:
      • id initialized with filterId
      • coeff initialized as {c0, c1, c2}
      • lastOutput initialized to 0
    • ❗ Assignment inside the constructor body is not allowed.
  • Public Methods
    • void apply(int x)
      • Updates the output using the formula:
        • lastOutput = x * coeff[0] + coeff[1] + coeff[2]; 
    • int read()
      • Returns the current value of lastOutput

In main()

  1. Read id, c0, c1, c2
  2. Construct a Filter3 object
  3. Read two input samples s1 and s2
  4. Call apply(s1) and then apply(s2)
  5. Print the final output using read()

 

Example Input

7 2 3 1
10 4

Example Output

12

Explanation

  • Coefficients = {2, 3, 1}
  • apply(10)10 * 2 + 3 + 1 = 24
  • apply(4)4 * 2 + 3 + 1 = 12
  • Final output = 12

 

Constraints

  • Overflow handling is not required
  • Input values remain within safe integer limits
  • Output must match exactly

 

 

 

Loading...

Input

7 2 3 1 10 4

Expected Output

12