edited by
6,745 views
25 votes
25 votes

Consider the following C program: 

  #include <stdio.h>
           #define EOF -1
           void push (int); /* push the argument on the stack */
           int pop  (void); /* pop the top of the stack */
           void flagError ();
           int main ()
          {         int c, m, n, r;
                     while ((c = getchar ()) != EOF)
                    { if  (isdigit (c) )
                               push (c);
                     else if ((c == '+') || (c == '*'))
                    {          m = pop ();
                                n = pop ();
                                r = (c == '+') ? n + m : n*m;
                                push (r);
                      }
                      else if (c != ' ')
                               flagError ();
             }
              printf("% c", pop ());
}


What is the output of the program for the following input?
$5 \ 2 \ * \ 3 \ 3 \ 2 \ + * +$

  1. $15$
  2. $25$
  3. $30$
  4. $150$
edited by

2 Answers

Best answer
31 votes
31 votes

Correct Option: B

$25$
let first part
$5$ ----push
$2$------push
push------$5*2=10$. (pops $5$ and $2$)

push $3$
push $3$
push $2$
push $3+2 = 5$ (pops $2$ and $3$)
push $5*3 = 15$ (pops ($5$ and $3$)
push $15 + 10 = 25$ (pops ($15$ and $10$)

edited by
Answer:

Related questions

62 votes
62 votes
11 answers
3
Ishrat Jahan asked Oct 29, 2014
28,941 views
Consider a hash function that distributes keys uniformly. The hash table size is $20$. After hashing of how many keys will the probability that any new key hashed collide...