Question & Answer: Consider the following code. What is printed by the code that follows the function definitions? Pay attention…..

10 Final Exam 11. (10 points) Consider the following code. What is printed by ure function definitions? Pay attention to when fprintf functions are triggered to function q -enqueue(v, q) 9-tv end v)i fprintf (enqueing ld into queue In. function Iv q) - dequeue(q) q(end); fprintf(deque ing%d from queuen q = q(1:end-1); end function s - push(v. ) s- (v s]; fprintf ( , pushing %d onto stackla, v); end function [v s] = pop(s) vs(1); fprintfC popping ld from stackin, v) s = s(2:end); end s1 = [1]; s2 = [23]; 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, sl) [v ql - dequeue (g); s1 = push(v, sl) (v q] = dequeue(q); 82 push(v, s2) (v s2] = pop(s2); s1 = push (v, sl) disp(s1); disp(s2); disp(q):

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 queuen’, v): end function [v q] = dequeue(q) v = q(end): fprintf(‘dequeing %d from queuen’, v): q = q(1: end-1): end function s = push(v, s) s = [v s]: fprintf(‘pushing %d onto stackn’, v): end function [v s] = pop(s) v = s(1): fprintf(‘popping %d from stackn, 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);

Expert Answer

 

Code;

function q = enqueue(v, q)
q = [v q]; fprintf(‘enqueing %d into queuen’, v);
end

function [v q] = dequeue(q)
v = q(end); fprintf(‘dequeing %d from queuen’, v);
q = q(1:end-1)’
end

function s = push(v, s)
s = [v s]; fprintf(‘pushing %d onto stackn’, v);
end

function [v s] = pop(s)
v = s(1); fprintf(‘popping %d from stackn’, v);
s = s(2:end);
end

s1 = [1];
s2 = [2 3];
q = [4 5];

%v = 2, s2 = [3]
[v s2] = pop(s2);

% q = [5 4 2]
q = enqueue(v, q);

% v=5, q = [4 2]
[v q] = dequeue(q);

% s2 = [5 3]
s2 = push(v, s2);

% v = 5, s2 = [3]
[v s2] = pop(s2);

% s1 = [5 1]
s1 = push(v, s1);

% v = 4, q = [2]
[v q] = dequeue(q);

% s1 = [4 5 1]
s1 = push(v, s1);

% v = 2, q = []
[v q] = dequeue(q);

% s2 = [2 3]
s2 = push(v, s2);

% v = 2, s2 = [3]
[v s2] = pop(s2);

% s1 = [2 4 5 1]
s1 = push(v, s1);

fprintf(“n”);

% 2 4 5 1
disp(s1);
% 3
disp(s2);
% []
disp(q);

Still stressed from student homework?
Get quality assistance from academic writers!