The Gateway to Computer Science Excellence
+24 votes
3.1k views

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 by Veteran (105k points) | 3.1k views
+1
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
0
Bcoz it is static!

3 Answers

+29 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

by Boss (33.8k points)
edited by
+31 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

by Veteran (62.8k points)
edited by
+2
Well explained answer :)
0
plz verify @Arjun sir ,  @Anirudh
stkFunc (0, 5); this function run  

case 0: if (stkTop < size ) A[stkTop++]=val; break here according to me sir due to this a poSt increment so first asssign the stkTop value means A[0]=5 then do stkTop=stkTop+1 means stkTop=1 and same thing will be happend for next steps. however answer we getting 15 in both case bcz they are not intrested the position of stack sir if did mistake plz tell me

+1
yes stkTop++ is post increement . You are going correct.
0
Your concept is correct.
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
does static variable overrides global variable?
+4 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.
by Boss (23.8k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,645 questions
56,616 answers
195,897 comments
102,355 users