Question: Consider the following code. (a) What is the final result of the code that follows the function definitions? (b) The pop function does not test
Consider the following code. (a) What is the final result of the code that follows the function definitions? (b) The pop function does not test to see if the stack is already empty. Add the code to test for this that returns 0 and the empty stack if this is the case. (c) Draw the parse tree for the expression in x (d) Express the expression in the usual infix notation. function [v s] = pop(s) s = [v s]: end function [v s] = pop(s) v = s(1): s = s(2: end): end x = {5, 14, 2, '*', 6, 4, '+', '-', 3, '+', '*'} stack = []: for ii = i: length (x) if isnumeric (x{ii}) stack = push(x(ii), stack) elseif ismember(x(ii), '+*-/' [a stack] = pop(stack): [b stack] = pop(stack): c = eval([sprintf(a) x(ii) sprintf(b)]): stack = push(c, stack): end end [v stack] = pop(stack): disp(v)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
