194k views
1 vote
I need help changing a previous code to work with another file and it has to be done in Mobaxterm I want a picture of the output.

Create a template class, called TBuffer, by copying a simple
IntegerBuffer class and replacing all references to the type of what it holds (originally int) with a template type T. The size of the
array must be given by a template parameter N. You must also add a new sum() member
function. The template will then be tested with ints, doubles, and strings.
Finally, in your implementation, some functions return a default value if the buffer is empty.
The sum() operation also needs an initial value appropriate to the type. Since we don’t know the
actual type, we can’t know what to use as a zero or starting value. A quick fix here is to have the
constructor take as an argument (T zeroValue) and assign it to a member variable specifically for
this role, called zeroValue, or nullValue, or something like that.
TBuffer.cpp
#include
#include
class IntegerBuffer {
protected:
int data[32];
int capacity;
int data_length;
int zero_value;
public:
IntegerBuffer() : capacity(32), data_length(0), zero_value(0) {};
int add(int value);
int add(const int array[], int length);
void print(int col_width, int row_width) const;
};
int IntegerBuffer::add(int value) {
if (dataLength < capacity) {
data[dataLength] = value;
++dataLength;
return 1;
}
else
return 0;
}
int IntegerBuffer::add(const int array[], int length) {
int count = 0;
for (int i = 0; i < length; ++i)
count += add(array[i]);
return count;
}
void IntegerBuffer::print(int col_width, int row_width) const {
for (int i = 0; i < dataLength; ++i) {
// check if value should start a new row
if (i % row_width == 0 && i > 0)
std::cout << '\\';
std::cout << std::setw(col_width) << data[i];
}
std::cout << std::endl;
}
hw4.cpp
#include
#include
#include "TBuffer.hpp"
using namespace std;

int main() {
TBuffer ib(0);
TBuffer db(0.0);
TBuffer sb("");
int input_data[10] = { 12, -4, 6, 8, 6, 5, -4, -6, 11, 9 };
ib.add(7);
ib.add(input_data, 9);
ib.add(12);
ib.add(17);
ib.print(5, 10);
cout << " Sum: " << ib.sum() << endl << endl;
db.add(8.463);
db.add(3.74);
db.add(5.231);
db.add(3.74);
db.print(7, 10);
cout << " Sum: " << db.sum() << endl << endl;
sb.add("Turing");
sb.add("Kleene");
sb.add("Church");
sb.add("Russell");
sb.add("Hoare");
sb.add("McCarthy");
sb.add("Chomsky");
sb.add("Frege");
sb.add("Jaffe");
sb.print(10, 7);
cout << " Sum: " << sb.sum() << endl;
}
The expected Ouptut and please a picture of your ouput on Mobaxterm.
The output should be as follows:
7 12 -4 6 8 6 5 -4 -6
Sum: 30
8.463 3.74 5.231 3.74
Sum: 21.174
Turing Kleene Church Russell Hoare McCarthy Chomsky
Frege Jaffe
Sum: TuringKleeneChurchRussellHoareMcCarthyChomskyFregeJaffe

User George T
by
7.6k points

1 Answer

5 votes

In this implementation, the TBuffer class is a template class that can be instantiated with different types (int, double, std::string) and different sizes (N).

#include <iostream>

#include <stdexcept>

template <typename T, int N>

class TBuffer {

private:

T buffer[N];

T nullValue;

public:

TBuffer(T nullValue) : nullValue(nullValue) {

// Initialize the buffer with null values

for (int i = 0; i < N; ++i) {

buffer[i] = nullValue;

}

}

void addElement(const T& element, int index) {

if (index < 0 || index >= N) {

throw std::out_of_range("Index out of range");

}

buffer[index] = element;

}

T getElement(int index) const {

if (index < 0 || index >= N) {

throw std::out_of_range("Index out of range");

}

return buffer[index];

}

T sum() const {

T result = nullValue;

for (int i = 0; i < N; ++i) {

result += buffer[i];

}

return result;

}

bool isEmpty() const {

for (int i = 0; i < N; ++i) {

if (buffer[i] != nullValue) {

return false;

}

}

return true;

}

};

User Korobko Alex
by
8.6k points