CS 211 prelim sp07

# CS 211 prelim sp07 - CS211 Spring 2007 Prelim 2 Solutions...

• Test Prep
• 9

This preview shows pages 1–4. Sign up to view the full content.

CS211 Spring 2007 Prelim 2 April 17, 2007 Solutions Instructions Write your name and Cornell netid above. There are 5 questions on 8 numbered pages. Check now that you have all the pages. Write your answers in the space provided. Indicate your answer clearly. Use the back of the pages for workspace. Ambiguous answers will be considered incorrect. The exam is closed book and closed notes. Do not begin until instructed. You have 90 minutes. Good luck! 1 2 3 4 5 Σ Score /20 /40 /20 /10 /10 /100 Grader

This preview has intentionally blurred sections. Sign up to view the full version.

April 17, 2007 CS211 Prelim 2 Page 1 of 8 1. (20 points) The expression 8 - 2 * 3 evaluates to 2, since it is understood as 8 - (2 * 3), following the convention that multiplication has precedence over subtraction. If we wish the operators to be evaluated in a different order, say as (8 - 2) * 3 = 18, we must include the parentheses explicitly. Expressions of this form containing operators such as + , * , - that appear between operands (the values they act upon) are called infix expressions. To evaluate such expressions, you either need to know the precedence ordering of the operators, or the expression must be parenthesized to help you interpret which operation to evaluate first. An alternative notation used to represent expressions is postfix notation . You do not need to learn any operator precedence rules to evaluate postfix expressions. You also do not need to use parentheses. In any postfix expression, all opera- tions appear after their corresponding operands, unlike infix, where they appear between their operands. And unlike infix, the order of evaluation of the operators is uniquely determined by the postfix expression. Due to its unambiguous inter- pretation, postfix notation is convenient to use as an intermediate representation in compilers and calculators. Some examples: Infix expression Postfix expression Value 2 + 3 2 3 + 5 7 * 8 7 8 * 56 6 / 2 + 5 6 2 / 5 + 8 5 + 6 / 2 5 6 2 / + 8 To evaluate a postfix expression, say for example 7 3 - 5 8 + * , you process the expression from left to right. Whenever you see an operator, you apply it on the two most recently seen operands, compute the result, and replace the operator and two operands by the result in the expression. For example, in the expression 7 3 - 5 8 + * , when we encounter the - , we compute 7 - 3, then replace the subexpression 7 3 - by 4. The whole expression now looks like 4 5 8 + * . We continue processing from left to right. This time, + is the first operator we encounter. We apply + to the two most recently seen operands, namely 5 and 8, which gives 4 13 * . Finally, this expression evaluates to 13 * 4, which is 52. (Questions on next page)
April 17, 2007 CS211 Prelim 2 Page 2 of 8 (a) Describe how to evaluate a postfix expression using a stack. Use English or high-level pseudocode, not Java. Assume that the operands and operators are available as a sequence of parsed tokens.

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.
• Summer '07
• FRANCIS

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern