Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ stash.h #ifndef STASH_H #define STASH_H #include #include struct Stash { int size; // Size of each space int quantity; // Number of storage spaces

image text in transcribed

C++

stash.h

#ifndef STASH_H #define STASH_H

#include #include

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 = 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

void* fetch(int index){ // Check index boundaries: assert(0 = 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

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions