Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that a program reads the following input values one at a time into variab

ID: 3871150 • Letter: A

Question

Assume that a program reads the following input values one at a time into variable X. 20 6 18 6 14 8 12 7 11 15 24 26 30 -99 Trace the following Java-like pseudocode code and write in the box its output as exactly it would appear on the screen. In the space below the box (and next page), show snapshots (drawings) of the stack and queue content as you trace the pseudocode. Create stack S Create queue Q S.push(50); Q.enqueue(50); Read first input value to X; While (X != -99) { Switch (X % 4) { Case 0: S.push(X) Exit Switch Statement; Case 1: If (S.isEmpty != true) { Y = S.top() Printout (“Stack Element: “ + Y); S.pop(); } Else Printout “Sorry, stack is empty”; Exit Switch Statement; Case 2: Q.enqueue(X); Exit Switch Statement; Case 3: If (Q.isEmpty != true) { Y = Q.front(); Printout (“Queue Element: “ + Y); Q.dequeue(); } Else Printout “Sorry, queue is empty”; Exit Switch Statement; } //end of Switch statement Read next input value to X; } //end of While statement Printout (“Stack Elements are: “); While ((S.isEmpty != true) { Y = S.top() Printout on newline (Y); S.pop(); } Printout (“Queue Elements are: “); While ((Q.isEmpty != true) { Y = Q.front() Printout on newline (Y); Q.dequeue(); }

Explanation / Answer

Assume that a program reads the following input values one at a time into variable X.
20 6 18 6 14 8 12 7 11 15 24 26 30 -99
Trace the following Java-like pseudocode code and write in the box its output as exactly
it would appear on the screen. In the space below the box (and next page), show snapshots
(drawings) of the stack and queue content as you trace the pseudocode.

Create stack S
Create queue Q
S.push(50);
Q.enqueue(50);
Read first input value to X;
While (X != -99)
{
Switch (X % 4)
{
Case 0: S.push(X) Exit Switch Statement;
Case 1: If (S.isEmpty != true)
       {
       Y = S.top()
       Printout (“Stack Element: “ + Y);
       S.pop();
       }
       Else
       Printout “Sorry, stack is empty”; Exit Switch Statement;
Case 2: Q.enqueue(X); Exit Switch Statement;
Case 3: If (Q.isEmpty != true)
       {
       Y = Q.front();
       Printout (“Queue Element: “ + Y);
       Q.dequeue();
       }
       Else
       Printout “Sorry, queue is empty”; Exit Switch Statement;
} //end of Switch statement
Read next input value to X;
} //end of While statement

20%4 = 0: Will be pushed to stack.
6%4 = 2: Will be enqueued to queue.
18%4 = 2: Will be enqueued to queue.
6%4 = 2: Will be enqueued to queue.
14%4 = 2: Will be enqueued to queue.
8%4 = 0: Will be pushed to stack.
12%4 = 0: Will be pushed to stack.
7%4 = 3: Y = 2.
So, at this point:
   S: 12   8   20
   Q: 6   18   6   14.
And the output is: Queue Element: 6
And after this:
   S: 12   8   20
   Q: 18   6   14.
11%4 = 3: Y = 2.
So, at this point:
   S: 12   8   20
   Q: 18   6   14.
And the output is: Queue Element: 18
And after this:
   S: 12   8   20
   Q: 6   14.  
15%4 = 2: Will be enqueued to queue.
24%4 = 0: Will be pushed to stack.
26%4 = 2: Will be enqueued to queue.
30%4 = 2: Will be enqueued to queue.
-99   This will exit the while loop, and will stop reading values.
At this point:
   S: 24   12   8   20
   Q: 6   14   15   26   30.
  

Printout (“Stack Elements are: “);
While ((S.isEmpty != true)
{
Y = S.top()
Printout on newline (Y);
S.pop();
}

And the output is:
Stack Elements are:
24
12
8
20


Printout (“Queue Elements are: “);
While ((Q.isEmpty != true)
{
Y = Q.front()
Printout on newline (Y);
Q.dequeue();
}
And the output is:
Queue Elements are:
6
14
15
26
30

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote