edited by
618 views
7 votes
7 votes

Consider a stack whose elements are unsigned integers and support the following operations:

  • PUSH a: Pushes the element 'a' onto the stack.
  • ADD: Adds the two topmost elements, removes them, and pushes the result.
  • SQR: Computes the square of the topmost element, removes it, and pushes the result.
  • REV: Reverses the order of the three topmost elements in the stack (if the stack contains fewer than three elements, REV is undefined).

Computation starts with an empty stack, and after a sequence of operations, the topmost element is considered the final result.
Given three unsigned integers $x, y$, and $z$, determine the number of instructions needed in the sequence to compute the following expression:
$$
(x+y)^2+z^2
$$
The instruction sequence should start with$: \textsf{PUSH x; PUSH y; PUSH z;} \ldots$
 

No further PUSH operations are allowed. After the entire sequence of instructions executes, the top of the stack should contain the given expression.

If an instruction is used more than once, count it as many times as it is used. Your final count should include initial 3 PUSH instructions.

edited by

1 Answer

5 votes
5 votes
PUSH x; PUSH y; PUSH z; SQR; REV; ADD; SQR; ADD;
edited by
Answer:

Related questions

589
views
2 answers
5 votes
GO Classes asked Feb 5
589 views
Let $B$ be a binary search tree (BST) with eight nodes filled with the following set of eight integer keys $A=\{10,2,5,3,20,15,9,22\}$. The order in which these keys were...
502
views
1 answers
4 votes
GO Classes asked Feb 5
502 views
What will be the output of the following C program?#include <stdio.h>void Mickey(int , int, int);void Mouse(int, int*);int main(){ int a = 2, b = 3, c = 4; int *poi...
1.0k
views
0 answers
4 votes
GO Classes asked Feb 5
1,002 views
The grammar shown below is LL(k) for some value of k. What is the smallest value of k for which this grammar is LL(k)?
511
views
1 answers
4 votes
GO Classes asked Feb 5
511 views
Consider the cache of size 512 bytes that is direct-mapped?Suppose the size of integer is 4 bytes and block size is 16 bytes. Assume cache is initially empty and all data...