SensorBuffer Fixed Storage

#include <iostream>
using namespace std;

// Define SensorBuffer class here
// <write your code>
class SensorBuffer{
    private:
        int id, count;
        int data[5];
    public:
        SensorBuffer(int id) : id(id), count(0){}
        void addSample(int v){
            if (this->count == 5) return;

            this->data[count++] = v;
        }

        int size(){
            return this->count;
        }

        void print() {
        for (int i = 0; i < count; i++) {
            cout << data[i];
            if (i + 1 < count) cout << " ";
        }
    }
};
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