SensorBuffer Fixed Storage

#include <iostream>
using namespace std;

// Define SensorBuffer class here
// <write your code>

class SensorBuffer {
    private:
    int id;
    int data[5];
    int count;

    public:
    explicit SensorBuffer(int id) noexcept : id(id), count(0) {};

    void addSample(int v) noexcept
    {
        if ( !( count == 5 )) data[count++] = v;
    }

    int size() const noexcept
    {
        return count;
    }

    void print() const noexcept
    {
        if ( count )
        {
            for (int i = 0; i < count; i++)
            {
                cout << data[i] << " " ;
            }
        }
    }
};
int main() {
    int id, n;
    cin >> id >> n;

    SensorBuffer buf(id);

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        buf.addSample(x);
    }

    buf.print();
    return 0;
}

Solving Approach

 

 

 


 

Upvote
Downvote
Loading...

Input

42 7 10 20 30 40 50 60 70

Expected Output

10 20 30 40 50