Consider the following code. What is printed by the code that follows the functi
ID: 3867573 • Letter: C
Question
Consider the following code. What is printed by the code that follows the function definitions? Pay attention to when fprintf functions are triggered. function q = enqueue (v, q) q = [v q]: fprintf('enqueing %d into queue ', v): end function [v q] = dequeue(q) v = q(end): fprintf('dequeing %d from queue ', v): q = q(1: end-1): end function s = push(v, s) s = [v s]: fprintf('pushing %d onto stack ', v): end function [v s] = pop(s) v = s(1): fprintf('popping %d from stack , v): s = (2: end): end s1 = [1]: s2 = [2 3]: q = [4 5]: [v s2] = pop(s2) q = enqueue(v, q) [v q] = dequeue(q): s2 = push(v, s2): [v s2] = pop(s2): s1 = push(v, s1) [v q] = dequeue(q): s1 = push(v, s1) [v q] =dequeue(q): s2 = push(v, s2) [v s2] = pop(s2) [v s2] = pop(s2): s1 = push(v, s1) disp(s1): disp(s2): disp(q);Explanation / Answer
First call to pop will pop 2 from stack and it wil print
"popping 2 from stack"
Second call will enque the value 2 in queue and will display
"enqueing 2 into queue"
Next call will dequeue 5 from queue and will display
"dequeing 5 from queue"
Similarly next line will be as
"pushing 5 onto the stack"
"popping 5 from stack"
"pushing 5 onto stack"
"dequeing 4 from queue"
"pushing 4 onto stack"
"dequeing 2 from queue"
"pushing 2 onto stack"
"poping 2 from stack"
"pushing 2 onto stack"
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.