B Iyer

Greenhorn
+ Follow
since Oct 13, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by B Iyer

Hi Embla, Vijitha and Ritchie,

Thank you very much for your reply and explanation. It is starting to make sense now.

Regards,
Biyer.
14 years ago
Hi Hunter,

Thanks for the reply. I do see what might probably be happening in the stack. How do I exit from a base case in a void method. I have a return without any value. It compiled fine. But is there a better way to do it?

Thanks,
bhanu.
14 years ago
Hello Vijitha,

Thanks for the reply. When I execute the piece of code in debug mode(presuming I have understood the question right and have the code right), what happens is that first it goes through the 1st printin method recursively, I can see the values of i decrement 16,8,4,2,1. So n == 1 is true, it executes return and hits the next printin method. But this time when printin executes n is 2. 2/2 is 1 so n ==1, so it hits return, but the stack is still there, so the 1st printin get called again and then it gets confusing. Does my code meet the requirements of the question?

Might sound very trivial, but I have been trying to figure it out for almost a day before I put my post..

Please help,

Thanks,
Bhanu.
14 years ago
Hi,

I have a question on recursion. It goes this way:

Suppose that we’re running a recursive algorithm where n is initially 16. Suppose that the
base case is n == 1. At each recursive step, two recursive method invocations are made with n/2.

a. How many total method invocations are needed (including the initial invocation)?
b. What are the most activation records that are on the stack at any one time during the
process?

I wrote a small piece of code. Is my code correctly portraying the question? If not, could some one please correct it? Could you please explain to me how the stack works. I have executed this program multiple times, but I don't understand the stack. I am now wondering if I have got the code right in the first place. Please help. Any input would be appreciated. Thanks very much

import java.io.*;
import java.lang.*;

public class printhalf
{
public static void main(String args[])
{
int i = 16;
printit(i);

}


public static void printit(int n)
{
if (n == 1)
return;
else
{
printit(n/2);
printit(n/2);

}
}

}

Thanks again,
biyer.
14 years ago