Question: in C++ Output should match example output #ifndef STASH_H #define STASH_H #include #include using namespace std; struct Stash { int size; // Size of each

in C++

Output should match example output

in C++ Output should match example output #ifndef STASH_H #define STASH_H #include

#ifndef STASH_H

#define STASH_H

#include

#include

using namespace std;

struct Stash {

int size; // Size of each space

int quantity; // Number of storage spaces

int next; // Next empty space

// Dynamically allocated array of bytes:

unsigned char* storage;

// Functions!

void initialize(int sz){

size = sz;

quantity = 0;

storage = 0;

next = 0;

}

void cleanup(){

if(storage != 0) {

std::cout

delete []storage;

}

}

int add(const void* element){

if(next >= quantity) // Enough space left?

inflate(100);

// Copy element into storage,

// starting at next empty space:

int startBytes = next * size;

unsigned char* e = (unsigned char*)element;

for(int i = 0; i

storage[startBytes + i] = e[i];

next++;

return(next - 1); // Index number

}

void* fetch(int index){

// Check index boundaries:

assert(0

if(index >= next)

return 0; // To indicate the end

// Produce pointer to desired element:

return &(storage[index * size]);

}

int count() {

return next; // Number of elements in CStash

}

void inflate(int increase){

assert(increase > 0);

int newQuantity = quantity + increase;

int newBytes = newQuantity * size;

int oldBytes = quantity * size;

unsigned char* b = new unsigned char[newBytes];

for(int i = 0; i

b[i] = storage[i]; // Copy old to new

delete []storage; // Old storage

storage = b; // Point to new memory

quantity = newQuantity;

}

}; ///:~

#endif

In the following exercise you will again use the Stash structure, however with two modi- fications: (a) you will have a new integer member to store the desired increment to be used during re-allocation (not a fixed 100 value (b) you will have another integer member variable to count the number of re-allocations (calls to the inflate method) the Stash structure needed during its use. You will now write a variation of the run-length encoder you wrote in the previous lab using the Stash structure with each entry being one character. First of all you will read as input an integer that will tell how much the Stash should use as increment. Then you will read a sequence of pairs, each pair containing a character and a number. For each pair (CN), add to a Stash object the character CN times without spaces. If a pair has a negative N number, then add the character C|N| times and then add a newline character. If a pair (&,99) is read, then stop reading values, print the elements in the Stash in the order received, and then print two numbers: the number of calls that were made to inflate() inside your Stash object, and the total size in bytes that was allocated by the Stash object at the end (the value of the quantity variable). Input 20 a 4 b 5 10 & 99 Output aaaabbbbbcccccccccc1 20 In the following exercise you will again use the Stash structure, however with two modi- fications: (a) you will have a new integer member to store the desired increment to be used during re-allocation (not a fixed 100 value (b) you will have another integer member variable to count the number of re-allocations (calls to the inflate method) the Stash structure needed during its use. You will now write a variation of the run-length encoder you wrote in the previous lab using the Stash structure with each entry being one character. First of all you will read as input an integer that will tell how much the Stash should use as increment. Then you will read a sequence of pairs, each pair containing a character and a number. For each pair (CN), add to a Stash object the character CN times without spaces. If a pair has a negative N number, then add the character C|N| times and then add a newline character. If a pair (&,99) is read, then stop reading values, print the elements in the Stash in the order received, and then print two numbers: the number of calls that were made to inflate() inside your Stash object, and the total size in bytes that was allocated by the Stash object at the end (the value of the quantity variable). Input 20 a 4 b 5 10 & 99 Output aaaabbbbbcccccccccc1 20

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!