Write the code for a C++ class called "Pile" which stores an array of
up to 100 integers (class prototyped below).
class Pile {
int items[100], displayIndex, currentIndex;
public:
Pile( );
Pile(const int *list, int n);
int operator++(int); // postfix operator
int addInteger(int p);
void setIndex(int si);
void show( );
};
A Pile has the following publicly accessible characteristics:
1. When created without any data, a Pile object sets all elements in its array to -1
and it's displayindex and currentIndex are both set to 0 .
2. A Pile may also be created by accepting a constant array of integers 'list' to copy from
and an integer 'n' that indicates the total number of elements in the
array that is passed in as a parameter. All elements in the 'list' array (up to 100)
and no more than 'n' are copied into the Pile's array. If fewer than 100 elements are
copied, then the remaining elements in the Pile's array are set to -1.
The displayIndex is set to 0 and the currentIndex is set to 'n' but ONLY if 'n' is <= 100,
otherwise currentIndex is set to 100.
.
3. The operator++ function increases EVERY element in the Pile array that is not equal
to -1 by 1 and returns the sum of all changed elements .
4. The addIndex function accepts an integer 'p' to ADD to the end of the Pile's array,
but only if the array is NOT already full.
If the integer can be added, the function increases currentIndex by 1 and returns 1,
otherwise the integer is not added, currentIndex is not changed, and the function
returns 0 .
5. The setIndex function accepts an integer 'si' and updates the Pile's displayIndex
member, but only if 'si' is within the range 0 to currentIndex inclusive or -1.
If this function sets displayIndex to -1, this means that your show( ) will
display the integers in the array in reverse order (starting from the last integer
added to the aray) .
6. The show function displays all integers (each separated by a space) in the Pile
array that are not -1 starting at index 'displayIndex'. The output MUST use the
C++ output style using cout and after all integers are displayed a newline is output.
If displayIndex is -1, then this function displays the integers in the array
in reverse order (starting from the last integer added to the aray)
.
MAIN PROGRAM:
The following main( ) program would produce the output below.
#include
using namespace std;
#define N 12
#define OVER 200
int main( ) {
int nums[N] = { 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35 };
int i, overflow[OVER], sum, rv;
for(i = 0; i < OVER; i++) {
overflow[i] = i * 2; // sets array values to 0, 2, 4, 6, 8, ... 396, 398
}
Pile x, y(nums, N), z(overflow, OVER); // z should only contain 100 values
x.addInteger(16);
x.addInteger(-8);
x.addInteger(451);
sum = x++; // increases all 3 values by 1 and returns sum
cout << "x Pile: ";
x.show( );
cout << "Pile x sum: " << sum << endl;
x.setIndex(-1);
cout << "x Pile: ";
x.show( );
cout << "Pile x sum: " << sum << endl;
y.setIndex(3);
sum = y++;
y.addInteger(39);
cout << "y Pile: ";
y.show( );
cout << "Pile y sum: " << sum << endl;
rv = z.addInteger(400); // should not be added (Pile array is already full)
z.setIndex(401); // should not change displayIndex (value 401 is out of bounds)
z.setIndex(90); // changes displayIndex to 90
cout << "rv is: " << rv << endl;
cout << "z Pile: ";
z.show( );
return 0;
}
The OUTPUT should be EXACTLY as displayed below:
x Pile: 17 -7 452
Pile x sum: 462
x Pile: 452 -7 17
Pile x sum: 462
y Pile: 12 15 18 21 24 27 30 33 36 39
Pile y sum: 234
rv is: 0
z Pile: 180 182 184 186 188 190 192 194 196 198