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

Here is Stash.h //----------------- #ifndef STASH_H #define STASH_H #include <ca

ID: 641383 • Letter: H

Question

Here is Stash.h

//-----------------

#ifndef STASH_H

#define STASH_H

#include <cassert>

#include <iostream>

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 << "freeing storage" << std::endl;

          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 < size; i++)

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

       next++;

  

       return(next - 1); // Index number

   }

   void* fetch(int index){

       // Check index boundaries:

       assert(0 <= index);

  

       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 < oldBytes; i++)

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

      

       delete []storage; // Old storage

       storage = b; // Point to new memory

       quantity = newQuantity;

   }

}; ///:~

#endif

Explanation / Answer

//main.cpp************************************************** #include "stash.h" #include #include using namespace std; int main() { Stash dblStash; dblStash.initialize(sizeof(double)); // (1) for(int i = 0; i < 5; i++) dblStash.add(&i); for(int j = 0; j
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote