STARTING OUT WITH C++ MPL
9th Edition
ISBN: 9780136673989
Author: GADDIS
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 18, Problem 14PC
Program Plan Intro
Stack Based Evaluation of Postfix Expression
Program Plan:
- Declare a function skipWhiteSpace that Skips whitespace in an input stream while evaluating a postfix expression.
- Declare a Function postFixEval that evaluates the postfix expression by considering if the next token in the input stream is an integer, read the integer and push it onto the stack using the push() operation of the stack .
- But if the input stream is an operator, pop the last two values from the stack using the pop operation and apply the operator, and push the result onto the stack and the lone value is the result.
- Declare the main function.
- Prompt the user to enter a postfix expression.
- Evaluate the postfix expression by calling the postFixEval function and print the result.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
A set of instructions may be used to show a function's return address. Remember that any modifications you make to the stack cannot prevent the procedure from returning to its caller.
Problem Description
The hailstone sequence is defined as the integer sequence that results from manipulating a positive
integer value n as follows:
If n is even, divide it by 2 (using floor division)
• If n is odd, multiply it by 3 and then add 1
Repeat this process until you reach 1.
For example, starting with n = 5, we get the sequence 5, 16, 8, 4, 2, 1.
If n is 6, we get the sequence 6, 3, 10, 5, 16, 8, 4, 2, 1.
If n is 7, we get 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.
If n is 8, we get the sequence 8, 4, 2, 1.
As far as anyone can tell, this process will eventually reach 1 for any starting value, although
mathematicians have been unable to formally prove this property as of yet.
a. Write a Python function named hail () that takes a single integer argument. hail() should
print out the sequence of numbers generated by applying the process above to the function
parameter. This function does not return any value.
C++ code
Screenshot and output is must
Chapter 18 Solutions
STARTING OUT WITH C++ MPL
Ch. 18.3 - Describe what LIFO means.Ch. 18.3 - What is the difference between static and dynamic...Ch. 18.3 - What are the two primary stack operations?...Ch. 18.3 - What STL types does the STL stack container adapt?Ch. 18 - Prob. 1RQECh. 18 - Prob. 2RQECh. 18 - What is the difference between a static stack and...Ch. 18 - Prob. 4RQECh. 18 - The STL stack is considered a container adapter....Ch. 18 - What types may the STL stack be based on? By...
Ch. 18 - Prob. 7RQECh. 18 - Prob. 8RQECh. 18 - Prob. 9RQECh. 18 - Prob. 10RQECh. 18 - Prob. 11RQECh. 18 - Prob. 12RQECh. 18 - Prob. 13RQECh. 18 - Prob. 14RQECh. 18 - Prob. 15RQECh. 18 - Prob. 16RQECh. 18 - Prob. 17RQECh. 18 - Prob. 18RQECh. 18 - Prob. 1PCCh. 18 - Prob. 2PCCh. 18 - Prob. 3PCCh. 18 - Prob. 4PCCh. 18 - Prob. 5PCCh. 18 - Prob. 6PCCh. 18 - Prob. 7PCCh. 18 - Prob. 8PCCh. 18 - Prob. 9PCCh. 18 - Prob. 10PCCh. 18 - Prob. 11PCCh. 18 - Prob. 12PCCh. 18 - Prob. 13PCCh. 18 - Prob. 14PCCh. 18 - Prob. 15PC
Knowledge Booster
Similar questions
- Python Language - Bad Luck Numbersarrow_forward(Numerical) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters to 0, and then generate a large number of pseudorandom integers between 0 and 9. Each time a 0 occurs, increment the variable you have designated as the zero counter; when a 1 occurs, increment the counter variable that’s keeping count of the 1s that occur; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of the time they occurred.arrow_forwardA sequence of instructions may be used to see the function's return address. Any modifications to the stack must be made with the return of the method to its caller in mind.arrow_forward
- 7. The following function converts a postfix expression to an infix expression assuming the expression can only contain unsigned integer numbers and +' and -' operators. 1. public static String convert (String postfix) { 2. String operand1, operand2, infix; 3. Stack s = new AStack; 4. int i = 0; 5. while (i < postfix.length) { char nextc = postfix.charAt (i); if ((nextc 6. 7. '+')|| (nextc == '-') { == operand2 = s.pop (); operandl = s.pop () ; infix = '(' + operandl + nextc + operand2 + ')'; s.push (infix); i++; 8. 9. 10. 11. } 12. else if (Character.isDigit (nextc)){ int start = i; while (Character.isDigit (postfix.charAt (i)) i++; infix = postfix.substring (start, i); s.push (infix); 13. 14. 15. 16. 17. } else i++; 18. 19. } 20 return s.pop () ; 21. } (a) Rewrite only the lines of code that need to be changed so that the above function converts a postfix expression to a prefix expression. (b) Describe in words what changes are needed on the above given convert () function so that it…arrow_forwardThe destination of a function's return value may be represented as a sequence of instructions. When making modifications to the stack, keep in mind that they must not prevent the method from returning to its caller.arrow_forwardslecetion sort (python) Selection sort is a sorting algorithm, like Bubble sort which you saw in the previous module. Selection sort works as follows: Selection sort divides the input list into two parts: a sublist of sorted items (left part) and a sublist of still unsorted items (right part). Initially, the sorted sublist is empty and the whole input consists of the unsorted sublist. To fill the sorted sublist, the algorithm computes the (index of) the minimum of the unsorted sublist and swaps the first unsorted element and the minimum element (if the minimum is already the first element, nothing happens). Afterward, the sorted sublist is one bigger. This continues until the unsorted sublist is empty and the entire input is sorted. Example: Sorted sublist Unsorted sublist Least element in unsorted list () (11, 25, 12, 22, 64) 11 (11) (25, 12, 22, 64) 12 (11, 12) (25, 22, 64) 22 (11, 12, 22) (25, 64) 25 (11, 12, 22, 25) (64) 64 (11, 12, 22, 25, 64) () Implement this…arrow_forward
- data structure in c++ pls do use more // comments especially the linked list partarrow_forwardA set of instructions may be used to show the return address of a function. Remember that any changes to the stack must not prevent the procedure's return to its caller.arrow_forwardIt is possible to see the function's return address by issuing a set of instructions. It's important to remember that modifying the stack shouldn't prevent the process from returning to its caller.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning