CS 211 prelim sp07 - CS211 Spring 2007 Prelim 2 April 17,...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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)
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

CS 211 prelim sp07 - CS211 Spring 2007 Prelim 2 April 17,...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online