in Algorithms retagged by
16,424 views
23 votes
23 votes

Consider the following C function.

void convert (int n ) {
        if (n<0)
            printf{“%d”, n);
        else {
            convert(n/2);
            printf(“%d”, n%2);
        }
}

Which one of the following will happen when the function convert is called with any positive integer $n$ as argument?

  1. It will print the binary representation of $n$ and terminate
  2. It will print the binary representation of $n$ in the reverse order and terminate
  3. It will print the binary representation of $n$ but will not terminate
  4. It will not print anything and will not terminate
in Algorithms retagged by
by
16.4k views

4 Comments

@mradul

you will discuss this point on https://gateoverflow.in/302822/gate2019-26

there is no need of posting this question again, right ?

0
0
i think it is a bonus any one  who can make it run program in compiler and let's check what the compiler gives the answer.

As I made run on my compiler i conclude that there is a non option is matched and it is to be a bonus. lets see what gate overflow will conclude .

Max the option will be nothing which was mentioned over there so it will be bonus marks.
3
3

I answered it correctly 2 years ago also.

3
3

8 Answers

31 votes
31 votes
Best answer

As per the question, the function convert needs to be called with a positive integer n as argument.

So, when a positive int is passed as argument, function will compute$(n/2)$ which will return the quotient of integer division. 

Again this result of integer division will be passed through compute$(n/2)$ and so on. Each time the no will be divided by $2$ and will gradually become smaller and smaller and close to 0 but it will never be less than 0

Hence, the program will not print anything (as after every function call, the value returned is greater than $0$) and will not terminate. 

PS: Being a C function and hence when run on a computer each recursive call will require creation of activation record consuming memory space. So, eventually the stack memory will run out and program terminates. But such an option is not there. D is the best option here.

Answer (D)

edited by

4 Comments

If by “if it terminates” you are implying that it doesn't terminate by runtime error, rather by the change in recursive condition ie

n<0

is changed to

n==0

Then it prints the binary representation of int n in reverse order.

 

1
1
if its equal prints binary of n in the normal order
1
1

no it will print it in reverse order.

0
0
32 votes
32 votes

I think none of the option matches. 

Ans should be :- "It won't print anything and terminates abruptly".

Remember here infinite looping is not there. It will terminate abruptly because of stackoverflow.

And thus for this question no option matches.

2 Comments

Agreed. This question is similar to this :

https://gateoverflow.in/118319/gate2017-1-36

5
5
At last what will be the answer either it is to be a bonus or it will not print anything and terminate please confirm me bro

Thanks for your valuable response
0
0
17 votes
17 votes
The print statement will run at the time n value becomes negative or function calls are over.

Divide by 2 won't give a negative result for any positive integer. So theoretically it will not terminate. Also, it won't print anything because printf statement in else part placed after the function call.

It will terminate due to StackOverflow without printing anything.
edited by

4 Comments

$C$ program it is. So it doesn't matter how much memory do you have. Ultimately it will terminate.
0
0
We can even run this program on a hypothetical machine with infinite memory. Any assumption of finiteness is just introducing data to the question, that does not exist.
0
0
the program will terminate with stackoverflow as infinite recursion is there and it won't print anything ..i think program will not terminate is not correct
0
0
2 votes
2 votes
Actually the correct answer for this question must be it terminates by showing error message stackoverflow as there is a limit upto which function calls can be pushed onto the stack and also it doesnot prints anything.And also infinite looping and stackoverflow are two different things so none of the option matches according to the given scenario.

2 Comments

will it be bonus??
0
0

@mradul

It might be a bonus...can see what GATE give to all..

1
1
Answer:

Related questions