Evaluate the following infix expression using algorithm and choose the correct…
2023
Evaluate the following infix expression using algorithm and choose the correct answer. a+b*c-d/e^f where a=1, b=2, c=3, d=4, e=2, f=2.
- A.
6
- B.
8
- C.
9
- D.
7
Show answer & explanation
Correct answer: A
Concept: In an infix expression, operators are evaluated in a fixed precedence order rather than strictly left to right across all operators. Exponentiation (^) is applied first, then multiplication and division are applied left to right, and finally addition and subtraction are applied left to right.
Substitute a=1, b=2, c=3, d=4, e=2, f=2 into a+b*c-d/e^f to get 1+2*3-4/2^2.
Evaluate the exponent first, since ^ has the highest precedence: ef = 22 = 4, so the expression becomes 1+2*3-4/4.
Evaluate multiplication and division next, left to right: b*c = 2*3 = 6, and d/(e^f) = 4/4 = 1, so the expression becomes 1+6-1.
Evaluate addition and subtraction last, left to right: 1+6 = 7, then 7-1 = 6.
Cross-check: fully parenthesizing by precedence gives a + (b*c) - (d/(e^f)) = 1 + 6 - 1 = 6, the same result obtained above.
Hence, the value of the expression is 6.