-4

There are some questions from my book which I am unable to understand, I hope you can help me explaining these.

  1. Consider following program fragment

    char c='a'
    while(c++<='z'
    putchar(xxx);
    

    If required output is abcd.....xyz,then xxx should be

    (a) c

    (b) c-2

    (c) c-1

    (d) --c

  2. What will be the value returned by the following function,when it is called with 11?

    recur(int num)
    {
    if((num/2)!=0)return(recur(num/2)*10+num%2);
    else return 1;
    }
    

    (a) Function does not return any value

    (b) 11

    (c) 1011

raymelfrancisco
  • 828
  • 2
  • 11
  • 21
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [how to ask](http://stackoverflow.com/questions/how-to-ask) – Eregrith Jun 05 '15 at 11:27
  • Try yourself first. If already did, tell us why and how it worked (or did not work). – Sourav Ghosh Jun 05 '15 at 11:27
  • 2
    `puchar()`? `while(c++<='z'`? Can you please atleast take your time to ask a question properly? – Sourav Ghosh Jun 05 '15 at 11:31
  • 1
    The first code snippet won't even compile – Spikatrix Jun 05 '15 at 11:31
  • @CoolGuy At it's present form, neither the last one (least, not properly). – Sourav Ghosh Jun 05 '15 at 11:32
  • i have put the questions as they were written in the book. They gave the answers as : (b) c-1 //for the first (c) 1011 //for the second one Now,i don't know how they did it and there is no explanation in the book.So i was only looking for some details on it Or just tell me whether these question are wrong by themselves. – gurpinder singh Jun 05 '15 at 11:42
  • No offence, but if this is how _exactly_ the code is presented in the book, I'll humbly request you to _keep aside_ (if not _throw away_)that book and try something else. – Sourav Ghosh Jun 05 '15 at 11:48
  • hahaha...... and they say this is one of the best book to prepare for GATE(GK publ.) – gurpinder singh Jun 05 '15 at 11:51

1 Answers1

1

Actually the sample code in 1 is very clear. It loops from 'a' to 'z', and putchar prints xxx, which should result from a to z. By the way, the number 1 code in proper form is this:

char c='a';
while(c++<='z') // c is incremented
   putchar(xxx); // lets say xxx is declared as char earlier

In 1, c is added by one already after it is being checked and just before it is printed (since post increment is done after everything in the line is done). So let's say c = 'a'. Is c less than or equal to z? Since it is, putchar() will be done, but before that, c is incremented, (c++) so the old value a will be b. So in order to print 'a' (the old value), you will print c - 1 ('b' - 1), which is 'a'.

So xxx = c - 1.

In question number 2, (a) is definitely not the answer.

Tracing recur():

// if recur(11)

recur(int num) {

   // num = 11, 11 / 2 is not 0
   if ((num / 2) != 0)

       // since num / 2 is not 0 because 11 / 2 = 5.5,
       // num will be (num / 2) since (num / 2) is passed as the new parameter
       // return(recur(5.5) * 10) + 5.5 % 2);
       // then repeat until num / 2 = 0
       // then compute the returns, (return_value * 10 + num % 2)
       return (recur(num / 2) * 10 + num % 2);
   else
       return 1;
}

This is how the values are changed:

num:     11.00
num / 2:  5.50

num:      5.00
num / 2:  2.50

num:      2.00
num / 2:  1.00

num:      1.00
num / 2:  0.50

return: 10
return: 101
return: 1011

final return: 1011
raymelfrancisco
  • 828
  • 2
  • 11
  • 21