UART Frame Builder

#include <iostream>
#include <cstdint>
using namespace std;

// Define class Frame with:
// - uint8_t buffer[16]
// - int size
// - operator+=
// - operator[]
// - getSize()
class Frame {
    private:
        uint8_t buffer[16];
        int size;
    public:
        Frame():size(0){}
        void operator+=(uint8_t b){
            buffer[size] = b;
            size++;
        }
        uint8_t& operator[](int& i){
            return buffer[i];
        }
        int getSize(){
            return size;
        }
};

int main() {
    int n;
    cin >> n;

    Frame frame;

    for (int i = 0; i < n; i++) {
        int b;
        cin >> b;
        frame += (uint8_t)b;
    }

    for (int i = 0; i < frame.getSize(); i++) {
        cout << (int)frame[i];
        if (i + 1 < frame.getSize()) {
            cout << " ";
        }
    }

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

4 10 20 30 40

Expected Output

10 20 30 40