CJCoding With Joseph
15free / daySign inCreate account

Fixed-Size Ring Buffer

Build a class RingBuffer that stores its values in an array<int, 4> (capacity 4) and never allocates anything else. A RingBuffer is created with no constructor arguments (the test writes RingBuffer rb;) and starts out empty.

Implement:
1. void add(int value): stores value. If fewer than 4 values are stored it is simply appended; if the buffer is full it replaces the oldest value.
2. int count() const: returns how many values are currently stored (0 to 4).
3. void print() const: prints the stored values from oldest to newest, separated by single spaces, followed by a newline. When the buffer is empty it prints just a newline (an empty line).

The test prints the result of count() on its own line and then calls print(), for example (see Expected Output).

One test also calls count() and print() on a brand-new empty buffer first, which must produce the line 0 followed by an empty line.

Do NOT write a main function.

Expected Output:

3
1 2 3
0

4
3 4 5 6

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
STL Containersstd::array
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.