65. SensorBuffer Fixed Storage

Create a class SensorBuffer that stores up to 5 sensor samples (integers).
This simulates a fixed-size sample buffer commonly used in embedded firmware where memory is limited.

Your class must:

  • Store the following private members:
    • int id → sensor ID
    • int data[5] → fixed-size buffer
    • int count → number of stored samples (range: 0–5)
  • Provide:
    • A constructor that sets id and initializes count = 0
    • void addSample(int v)
      • If the buffer is full (count == 5), ignore the new value
      • Otherwise, store the value at index count and increment count
    • int size()
      • Returns the number of stored samples
    • void print()
      • Prints all stored samples in insertion order, space-separated
      • If empty, print nothing
  • In main():
    • Read id
    • Read n (number of incoming samples)
    • Read n integer samples
    • Add each sample using addSample()
    • Print the stored samples using print()

Example Input:

42
7
10 20 30 40 50 60 70

Example Output:

10 20 30 40 50

Why?

  • The buffer can store only 5 values.
  • Any additional samples are ignored.

 

Constraints:

  • All member variables must be private
  • Maximum stored samples = 5
  • No dynamic memory allocation allowed
  • Samples must be stored in insertion order
  • Print only the stored values

 

 

 

 

Loading...

Input

42 7 10 20 30 40 50 60 70

Expected Output

10 20 30 40 50