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: 
        SensorBuffer(int i) : id(i), count(0) {}
        void addSample(int v) {
            if (count < 5) {
                data[count] = v; 
                count++; 
            }
        }
        int size() {
            return 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