in DS
8,153 views
35 votes
35 votes

Consider the C program below

#include <stdio.h>
int *A, stkTop;
int stkFunc (int opcode, int val)
{
    static int size=0, stkTop=0;
    switch (opcode) {
        case -1: size = val; break;
        case 0: if (stkTop < size ) A[stkTop++]=val; break;
        default: if (stkTop) return A[--stkTop];
    }
    return -1;
}
int main()
{
    int B[20]; A=B; stkTop = -1;
    stkFunc (-1, 10);
    stkFunc (0, 5);
    stkFunc (0, 10);
    printf ("%d\n", stkFunc(1, 0)+ stkFunc(1, 0));
}

The value printed by the above program is ________.

in DS
8.2k views

2 Comments

in this question the scope of stkTop is local to the function then while calling another time why it is not 0.

someone pls explain this
1
1
Bcoz it is static!
3
3

4 Answers

37 votes
37 votes
Best answer

Answer: $15$

The code is pushing $5$ and $10$ on stack and then popping the top two elements and printing their sum.

http://ideone.com/kIUdQT

edited by
45 votes
45 votes

Initially stack is empty = -1

stkFunc (-1, 10); this function

case -1: size = val; break; and static size= 10 // size memory declare one time only// and control comes out of switch b/c of break

stkFunc (0, 5); this function run

case 0: if (stkTop < size ) A[stkTop++]=val; break; here stktop is static value so memory declare at compile time only now check if condition 0< 10 true then A[stktop++== A[0+1]=val= 5 i.e. push 5 into stack break comes so control comes outside

stkFunc (0, 10); this comes

case 0: if (stkTop < size ) A[stkTop++]=val; break; same as above make A[stkTop++]= 10 i,e. push 10 into stack and break comes so control comes outside

printf ("%d\n", stkFunc(1, 0)+ stkFunc(1, 0));

this function  

stkFunc(1, 0) this will run

default: if (stkTop) return A[--stkTop] return top of stack which is 10

stkFunc(1, 0) this will run

default: if (stkTop) return A[--stkTop] return top of stack which is 5

printf ("%d\n", stkFunc(1, 0)+ stkFunc(1, 0));= 5+10=15 15 will be printed

edited by

4 Comments

Your concept is correct.
0
0
how are A [stacktop++] and A [--stacktop] different? is there any case of post or pore increment? if so then A [--stacktop] should pop 5 first and then stop at index before 0.
0
0
does static variable overrides global variable?
0
0
6 votes
6 votes
By Analyzing the above code it shows that.. this is simply stack implementation with 3 choices:-

Case (-1):-->>[Set Size of the Stack]

Code :-->>size = val

Case (0):-->>[ Push Operation]

                 Step1:- verify stack is full or not?

                 Step2:-  If not full then push the  element  in stkTop And increment stkTop.

                  Code:-->>if (stkTop < size )    //   step1

                  A[stkTop++]=val;  //step 2+3

Case (default):--(here when 1 is as choice)[Pop Operation]

                  step1:-> if stack is not empty thenPop the element from stkTop

                 Step2:->decrement stkTop

                 Code:->>if (stkTop) return A[--stkTop];// step 1+2

 

So simply in this program

1.set stack size 10

2.push 2 elements in top of the stack as

5 and 10

3.pop both ;then add them

So result is 15.

PS:->>But in exam we should directly trace the code and write the answer.
1 vote
1 vote

Thus, answer is 15.

Answer:

Related questions