1. Create a new method on a Stack that determines if all the values on the Stack
ID: 3680832 • Letter: 1
Question
1. Create a new method on a Stack that determines if all the values on the Stack are smaller than the passed value. For example, for a stack containing top- (1) (1) (2) (3) (5) (5) (2), allSmaller( 3 ) should return false because there values on the Stack bigger than 3. On the other hand, for a stack containing top- (1) (1) (2) (3) (5) (5) (2), allSmaller( 12 ) should return true because all the values on the stack are less than 12.
This new operation should have the signature:
bool Stack<Object>::allSmaller( const Object & data ) const;
Because it has been marked const, this method is saying that it is read-only. When your implementation runs, you won't be able to actually change any data members of the Stack passed to your method. Create a test driver program that exercises each of these exercises to satisfy yourself that your implementation is correct. For example, you should print the Stack before and after invoking the operation.
TESTING HINT:
Run the methods: push( 3 ); push( 2 ); push( 1 );
Print the stack. What should it look like?
Run the method: allSmaller( 1 ). It should return false. Does it?
Run the method: allSmaller( 10 ). It should return true. Does it?
Run the methods: push( 4 ); push( 100 ); push( 1 );
Print the stack. What should it look like?
Run the method: allSmaller( 1 ). It should return false. Does it?
Run the method: allSmaller( 100 ). It should return false. Does it?
Run the methods: pop(); pop();
Print the stack. What should it look like?
2. Create a new method on a Stack that determines if how many times a passed value is found on the Stack. For example, for a stack containing top- (1) (1) (2) (3) (5) (5) (2), count( 3 ) should return 1 because it is there just once on the Stack. On the other hand, for a stack containing top- (1) (1) (2) (3) (5) (5) (2), count( 12 ) should return 0 because there are no values on the stack that equal 12.
This new operation should have the signature:
int Stack<Object>::count( const Object & data ) const;
Because it has been marked const, this method is saying that it is read-only. When your implementation runs, you won't be able to actually change any data members of the Stack passed to your method. Create a test driver program that exercises each of these exercises to satisfy yourself that your implementation is correct. For example, you should print the Stack before and after invoking the operation.
TESTING HINT:
Run the methods: push( 3 ); push( 2 ); push( 1 );
Print the stack. What should it look like?
Run the method: count( 1 ). It should return 1. Does it?
Run the method: count( 0 ). It should return 0. Does it?
Run the methods: push( 4 ); push( 100 ); push( 1 );
Print the stack. What should it look like?
Run the method: count( 1 ). It should return 2. Does it?
Run the method: count( 100 ). It should return 1. Does it?
Run the methods: pop(); pop();
Print the stack. What should it look like?
Explanation / Answer
Answer 1:
TESTING HINT:
Run the methods: push( 3 ); push( 2 ); push( 1 );
Print the stack. What should it look like?
Answer:
Stack contents are 3,2,1 , here 1 is top value.
Run the method: allSmaller( 1 ). It should return false. Does it?
Answer:
yes ,because in stack already 3 and 2 are there which are greater than 1, so it should return false.
Run the method: allSmaller( 10 ). It should return true. Does it?
Answer:
Yes it returns true, because all the values present in the stack are smaller than the 10.
Run the methods: push( 4 ); push( 100 ); push( 1 );
Print the stack. What should it look like?
Answer:
If we are inserting the values on the same stack then the output is like
3,2,1,4,100,1 otherwise if we insert the values in new stack values are 4,100,1
Run the method: allSmaller( 1 ). It should return false. Does it?
Answer : Yes ,because in stack already 4 and 100 are there which are greater than 1,so it should return false.
Run the method: allSmaller( 100 ). It should return false. Does it?
Answer : No, because there are no greater values than 100, already 100 is there which is equivalent.
Run the methods: pop(); pop();
Answer: Output is 1 and 100 because stack follows LAST IN FIRST OUT method. Lastly inserted element is 1 and last but one element is 100 so first we will get an element 1 and then 100.
Print the stack. What should it look like?
Answer: if we consider all these questions as a single stack the values remaining in the stack are 3,2,1,4 because the values 1 and 100 are popped in the previous question.
Question2:
TESTING HINT:
Run the methods: push( 3 ); push( 2 ); push( 1 );
Print the stack. What should it look like?
Answer: stack elements are 3,2,1. 1 is on top of the stack.
Run the method: count( 1 ). It should return 1. Does it?
Answer: yes . it should return 1 because in stack 1 is appeared only once.
Run the method: count( 0 ). It should return 0. Does it?
Answer: Yes, because the element 0 is not at all inserted so it returns 0.
Run the methods: push( 4 ); push( 100 ); push( 1 );
Print the stack. What should it look like?
Answer: output 3,2,1,4,100,1, here 1 is top most element which is inserted last.
Run the method: count( 1 ). It should return 2. Does it?
Answer: yes it should return 2 because in stack elements are 3,2,1,4,100,1. In this output 1 is appeared twice.
Run the method: count( 100 ). It should return 1. Does it?
Answer:
yes . it should return 1 because in stack 100 is appeared only once.
Run the methods: pop(); pop();
Print the stack. What should it look like?
Answer: the elements in the stack are 3,2,1,4,100,1, so when we popped the elements the popped elements are 1 and 100, because top most element is 1 so it is popped out first and then 100.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.