Circular Buffer Indexing

#include <iostream>

class AudioBuffer {
private:
    int head_index;
public:

    AudioBuffer() : head_index(0) {}

    void advanceHead(int steps){
        head_index = ((head_index + steps)%8);
    }


    int get_index(){
        return head_index;
    }
};

int main() {
    int N;
    if (!(std::cin >> N)) return 0;

    AudioBuffer buffer;

    for (int i = 0; i < N; ++i) {
        int steps;
        std::cin >> steps;

        buffer.advanceHead(steps); 

        std::cout << "Head: " << buffer.get_index() << std::endl;
    }

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

3 5 4 10

Expected Output

Head: 5 Head: 1 Head: 3