//
7. Make a Stash that holds doubles. Fill it with 25 double
// values, then print them out to the console.
#ifndef SOLUTION07_H
#define SOLUTION07_H
struct Stash {
int quantity;
int index;
double * storage;
void initialize ();
void inflate ( int increase);
int add ( double n);
double fetch( int index);
void cleanup ();
};
#endif // SOLUTION07_H
// values, then print them out to the console.
#ifndef SOLUTION07_H
#define SOLUTION07_H
struct Stash {
int quantity;
int index;
double * storage;
void initialize ();
void inflate ( int increase);
int add ( double n);
double fetch( int index);
void cleanup ();
};
#endif // SOLUTION07_H
//
7. Make a Stash that holds doubles. Fill it with 25 double
// values, then print them out to the console.
#include < iostream >
#include " solution07.h "
using namespace std;
const int increase = 25 ;
int main () ... {
double t = 25.1;
Stash number;
number.initialize();
for (int i = 0; i < 25; i++)
number.add(t + i);
for (i = 0; i < 25; i++)
cout << number.fetch (i) << ' ';
cout << endl;
number.cleanup();
}
// values, then print them out to the console.
#include < iostream >
#include " solution07.h "
using namespace std;
const int increase = 25 ;
int main () ... {
double t = 25.1;
Stash number;
number.initialize();
for (int i = 0; i < 25; i++)
number.add(t + i);
for (i = 0; i < 25; i++)
cout << number.fetch (i) << ' ';
cout << endl;
number.cleanup();
}
//
7. Make a Stash that holds doubles. Fill it with 25 double
// values, then print them out to the console.
#include < iostream >
#include " solution07.h "
using namespace std;
const int increase = 5 ;
void Stash::initialize () {
quantity = 0 ;
index = 0 ;
storage = 0 ;
}
void Stash::inflate ( int increase) {
int oldquantity = quantity;
int newquantity = oldquantity + increase;
double * old = storage;
storage = new double [newquantity];
for ( int i = 0 ; i < oldquantity; i ++ )
storage[i] = old[i];
delete []old;
quantity = newquantity;
}
int Stash::add ( double n) {
if (index >= quantity)
inflate(increase);
storage[index] = n;
index ++ ;
return index;
}
double Stash::fetch( int index) {
return storage[index];
}
void Stash::cleanup () {
cout << " freeing storage... " << endl;
int quantity = 0 ;
int index = 0 ;
delete []storage;
}
// values, then print them out to the console.
#include < iostream >
#include " solution07.h "
using namespace std;
const int increase = 5 ;
void Stash::initialize () {
quantity = 0 ;
index = 0 ;
storage = 0 ;
}
void Stash::inflate ( int increase) {
int oldquantity = quantity;
int newquantity = oldquantity + increase;
double * old = storage;
storage = new double [newquantity];
for ( int i = 0 ; i < oldquantity; i ++ )
storage[i] = old[i];
delete []old;
quantity = newquantity;
}
int Stash::add ( double n) {
if (index >= quantity)
inflate(increase);
storage[index] = n;
index ++ ;
return index;
}
double Stash::fetch( int index) {
return storage[index];
}
void Stash::cleanup () {
cout << " freeing storage... " << endl;
int quantity = 0 ;
int index = 0 ;
delete []storage;
}