. Make the following changes to the stack.h implementation file Change the data
ID: 3685103 • Letter: #
Question
. Make the following changes to the stack.h implementation file
Change the data type used in the stack from char to int. Make any necessary changes throughout the file to make this work properly.
Add the following member functions to the class:
// peek returns the top element of the stack
// without changing the stack. peek returns -1
// when the stack is empty.
int peek ();
// elements returns the number of elements in the
// stack. elements should not change the stack in
// any way.
int elements ();
// print prints the elements in the stack from the
// top element to the bottom. It prints the
// elements one per line. It does not print any
// other text. print should not change the stack in
// any way.
void print ();
None of the above functions should print any error messages.
2. Write an application program, bigtop.cpp, that uses the stack class.
There will be two stacks. The first stack, ups, will have values that are increasing from bottom to top. The second stack, downs, will have values that are decreasing from bottom to top.
The program will repeatedly generate random numbers between 1 and some constant, biggest, and place them on either the ups stack or the downs stack. The program will stop when a number is generated that will not fit on either stack.
We would like to place as many numbers on the stacks as possible. To accomplish this, the following pseudo code should be used for deciding which stack to put the number on:
if (number is greater than biggest/2 and can be
placed on downs)
put the number on downs
else
if (number is less than biggest/2 and can be placed
on ups)
put the number on ups
else
if (number can be put on downs)
put the number on downs
else
if (number can be put on ups)
put the number on ups
When a number fails, the program should print both stacks (labeled with the number of elements in each printed) and the number that would not fit on either of the stacks.
Here is a sample run with biggest equal to 1000:
The final going up stack is with 2 elements is:
496
460
The final going down stack with 4 elements is:
127
544
580
931
The number that failed is 277
Stack.h Code
---------------------
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "Stack.h"
using namespace std;
int main(){
srand(time(0));
stack ups;
stack downs;
int biggest = 1000, num;
while(true){
num = rand() % biggest;
cout << num << " ";
if(num > biggest / 2 && (downs.empty() == 1 || num < downs.peek()) && downs.elements() < stack_size){
cout << "--1 ";
downs.push(num);
}
else if(num < biggest / 2 && num > ups.peek() && ups.elements() < stack_size){
cout << "--2 ";
ups.push(num);
}
else if((downs.empty() == 1 || num < downs.peek()) && downs.elements() < stack_size){
cout << "--3 ";
downs.push(num);
}
else if(num > ups.peek() && ups.elements() < stack_size){
cout << "--4 ";
ups.push(num);
}
else break;
}
cout << "The final going up stack is with " << ups.elements() << " elements is: ";
ups.print();
cout << "The final going down stack is with " << downs.elements() << " elements is: ";
downs.print();
cout << "The number that failed is " << num << " ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.