Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The problem is in C++ and I am very lost on what to do, any comments in the code

ID: 3740336 • Letter: T

Question

The problem is in C++ and I am very lost on what to do, any comments in the code would be greatly appreciated. Thanks!

circbuf.h

#ifndef CIRCBUF_H

#define CIRCBUF_H

#include<iostream>

using std::ostream;

#include<vector>

using std::vector;

#include<initializer_list>

using std::initializer_list;

class CircBuf{

private:

int sz_;

int cnt_;

vector<long> buf_;

size_t head_;

size_t tail_;

public:

CircBuf(size_t s=10);

CircBuf(initializer_list<long>, size_t);

long front() const;

long back() const;

bool full() const;

bool empty() const;

void add(long);

void remove();;

friend ostream& operator<<(ostream&, const CircBuf &cb);

/*

friend CircBuf operator+(CircBuf &buf, long val);

friend CircBuf operator+(long val, CircBuf &buf);

friend CircBuf operator+(CircBuf &buf2, CircBuf &buf1);

*/

};

ostream& operator<<(ostream&, const CircBuf &cb);

/*

CircBuf operator+(CircBuf &buf, long val);

CircBuf operator+(long val, CircBuf &buf);

CircBuf operator+(CircBuf &buf2, CircBuf &buf1);

*/

#endif


main.cpp

#include<iostream>
using std::cout; using std::cin; using std::endl;
#include<iomanip>
using std::boolalpha;
#include<string>
using std::string;
#include<stdexcept>
using std::runtime_error;

#include "lab10_circbuf.h"

int main(){
cout << boolalpha;

CircBuf cb(4);
cout << boolalpha;
cout << cb << endl;

CircBuf cb_il({2,4,6,8}, 5);
cout << cb_il << endl;

try{
    CircBuf bad_il({2,4,6,8}, 4);
}
catch (runtime_error &e){
    cout << e.what() << endl;
}

cb.add(11);
cb.add(12);
cout << cb << endl;

cout << "Front:" << cb.front() << endl;
cout << "Back:" << cb.back() << endl;

cb.remove();
cout << "Front:"<<cb.front() <<endl;
cb.remove();
cout << "Empty?:"<< cb.empty() << endl;
cout << "Full?:" << cb.full() << endl;

cout << "Add 4 elements"<<endl;
for(long i=0; i<4;i++)
    cb.add(i+27);
cout << cb << endl;

cout << "Remove 4 elements"<<endl;
while(!cb.empty()){
    cb.remove();
}

cout << "Fill er up"<<endl;
for(int i=0; !cb.full(); i++)
    cb.add(i*i);
cout << "Full?:"<<cb.full() <<endl;

// should throw, it's full
try{
    cb.add(1111);
}
catch (runtime_error err){
    cout << err.what() << endl;
}

cout << "Drain it"<<endl;
for(int i=0; !cb.empty(); i++)
    cb.remove();
cout << "Empty?:"<<cb.empty()<<endl;

// should thow, it's empty
try{
    cout << cb.front() << endl;
}
catch (runtime_error err){
    cout << err.what() << endl;
}

CircBuf cb2({1,2,3,4}, 5);
cb2.add(20);
cb2.remove();
cb2.add(30);
cb2.remove();
cout << cb2 << endl;

/*
// Extra Credit
cb = cb + 25;
cb = 30 + cb;
cout << cb << endl;
cb = cb + cb;
cout << cb << endl;
*/
}

The Problem We are going to work on making our own classes with private data members and special accessors We are going to build a CircularBuffer class, a common data structure with well known accessors Some Background Our CircularBuffer will be a data structure that stores long. A CircularBuffer is a fixed size FIFO (First In, First Out) data structure. It is essentially a line (a queue). First thing added (the Head position in the diagram) is the first thing read. The next thing added is at the Tail position. It is the last thing added, the last thing that will be readread. The underlying data structure for this approach has a fixed size data structure. It can become empty, it can become full. It does not grow or shrink in size over the course of the run of the program. Head (extract) IFO asa Circular Buffer Tail (insert) Things you can do with your CircularBuffer you can add to the CircularBuffer. An element is added at the Tail position. The write position is then advanced (clockwise in the diagram you can remove an element. The element at the Head position is removed. The Head position is then advanced (clockwise in the diagram) you can test if it is full e you can test if it is empty you can report the front element . you can report the back element Your Tasks We are going to make a CircBuf class with these characteristics and test it. The Class The CircBuff class will have an underlying data member buf of type vector of fixed size This is a private data member, which will represent the underlying data array. buf is private, you cannot access it from a main program using the class Data Members * int sz (the maximum size of the fixed size buffer)

Explanation / Answer

//main.cpp
#include<iostream>
using std::cout; using std::cin; using std::endl;
#include<iomanip>
using std::boolalpha;
#include<string>
using std::string;
#include<stdexcept>
using std::runtime_error;

#include "lab10_circbuf.h"

int main(){

cout << boolalpha;
CircBuf cb(4);
cout << boolalpha;
cout << cb << endl;
CircBuf cb_il({2,4,6,8}, 5);
cout << cb_il << endl;
try{
    CircBuf bad_il({2,4,6,8}, 4);
}
catch (runtime_error &e){
    cout << e.what() << endl;
}

cb.add(11);
cb.add(12);
cout << cb << endl;
cout << "Front:" << cb.front() << endl;
cout << "Back:" << cb.back() << endl;
cb.remove();
cout << "Front:"<<cb.front() <<endl;
cb.remove();
cout << "Empty?:"<< cb.empty() << endl;
cout << "Full?:" << cb.full() << endl;

cout << "Add 4 elements"<<endl;
for(long i=0; i<4;i++)
    cb.add(i+27);
cout << cb << endl;
cout << "Remove 4 elements"<<endl;
while(!cb.empty()){
    cb.remove();
}
cout << "Fill er up"<<endl;
for(int i=0; !cb.full(); i++)
    cb.add(i*i);
cout << "Full?:"<<cb.full() <<endl;
// should throw, it's full
try{
    cb.add(1111);
}
catch (runtime_error err){
    cout << err.what() << endl;
}

cout << "Drain it"<<endl;
for(int i=0; !cb.empty(); i++)
    cb.remove();
cout << "Empty?:"<<cb.empty()<<endl;
// should thow, it's empty
try{
    cout << cb.front() << endl;
}
catch (runtime_error err){
    cout << err.what() << endl;
}
CircBuf cb2({1,2,3,4}, 5);
cb2.add(20);
cb2.remove();
cb2.add(30);
cb2.remove();
cout << cb2 << endl;

// // Extra Credit
// cb = cb + 25;
// cb = 30 + cb;
// cout << cb << endl;
// cb = cb + cb;
// cout << cb << endl;


}
----------------------------------------------------------------------
//lab10_circbuf.cpp
#include<iostream>
using std::cout; using std::cin; using std::endl;
#include<iomanip>
using std::boolalpha;
#include<string>
using std::string;
#include<stdexcept>
using std::runtime_error;
#include <list>
using std::list;
#include<sstream>
using std::ostringstream;

#include "lab10_circbuf.h"

CircBuf::CircBuf(size_t sz)
{
    sz_ = sz;
    buf_ = vector<long>(sz);
    cnt_ = 0;
    head_ = 0;
    tail_ = 0;
    //everything else is set to zero (cnt_, head_, tail_)
}

//if the parameter size is smaller than initializationlist, throw a runtime_errr
CircBuf::CircBuf(initializer_list<long> li, size_t sz)
{
    if (sz < li.size()) {
        throw runtime_error("runtime error");
    }
    buf_ = vector<long> (sz);

    copy(li.begin(), li.end(), buf_.begin());
    cnt_ = li.size();
    tail_ = cnt_ + 1;
}

bool CircBuf::full() const
{
    if (cnt_ == sz_) {
        return true;
    }
    else {
        return false;
    }
}

bool CircBuf::empty() const
{
    if (cnt_ == 0) {
        return true;
    }
    else {
        return false;
    }
}

long CircBuf::front() const
{
    if (cnt_ == 0) {
        throw runtime_error("runtime error");
    }

    else {
        return buf_[head_];
    }
}

long CircBuf::back() const
{
    if (cnt_ == 0) {
        throw runtime_error("runtime error");
    }

    else {
        size_t bufindex = (tail_ -1);
        if (bufindex < 0) {
            return (buf_[sz_ -1]);
        }
        else {
            return buf_[tail_ -1];
        }
    }
}


void CircBuf::add(long ele)
{
    if (buf_.full()) {
        throw runtime_error("runtime error");
    }
    else {

        buf_[tail_] = ele;

    }

}

void CircBuf::remove()
{
    if (buf_.empty()) {
        throw runtime_error("runtime error");
    }
    else {
        head_ = head_ +1;
        if (head_ >= sz_) {
            head_ = 0;
            cnt_ = cnt_ -1;
        }
    }
}


//Front:3, Back:30, Cnt:4, Sz:5 30, 2, 3, 4, 20
ostream& operator<<(ostream&, const CircBuf &cb)
{
    ostringstream oss;

    if (cb.empty()) {
        cout << ("CircBuf empty") << endl;
    }

    else {
        oss << "Front:" << cb.front() << ", " << "Back:" << cb.back() << ", " << "Cnt:" << cb.cnt_ << ", " << "Sz:" << cb.sz_ << " " << endl;
    }

}
-------------------------------------------------------------------------------
//lab10_circbuf.h
#ifndef CIRCBUF_H
#define CIRCBUF_H

#include<iostream>
using std::ostream;
#include<vector>
using std::vector;
#include<initializer_list>
using std::initializer_list;
#include<sstream>
using std::ostringstream;

class CircBuf{
private:
    int sz_;
    int cnt_;
    vector<long> buf_;
    size_t head_;
    size_t tail_;

public:
    CircBuf(size_t s=10);
    CircBuf(initializer_list<long>, size_t);
    long front() const;
    long back() const;
    bool full() const;
    bool empty() const;
    void add(long);
    void remove();;

    friend ostream& operator<<(ostream&, const CircBuf &cb);
    /*
    friend CircBuf operator+(CircBuf &buf, long val);
    friend CircBuf operator+(long val, CircBuf &buf);
    friend CircBuf operator+(CircBuf &buf2, CircBuf &buf1);
    */
};

ostream& operator<<(ostream&, const CircBuf &cb);
/*
CircBuf operator+(CircBuf &buf, long val);
CircBuf operator+(long val, CircBuf &buf);
CircBuf operator+(CircBuf &buf2, CircBuf &buf1);
*/

#endif