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 id){
        this->id = id;
        this->count = 0;
    }
    void addSample(int v){
        if(this->count < 5){
            this->data[count] = v;
            this->count++;
        }
    }
    int size(){
        return this->count;
    }
    void print(){
        // cout << this->id <<"\n";
        for(int i = 0; i < this->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