• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursive call question  RSS feed

 
Preethi Chilukuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class RecTest {
int values[];

RecTest(int i){
values = new int[i];
}

void printArray(int i) {
if (i==0) return;
else printArray(i-1);
System.out.println("[" + (i-1) + "] " + values[i-1]);
}
}


public class Test {

public static void main(String args[]) {

RecTest ob = new RecTest(10);

int i;

for(i=0; i<10; i++) ob.values[i] = i;

ob.printArray(10);

}
}
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have forgotten to ask a question?

You also forgot to put your code in Code Tags when posting, as you can see it is much more readable when you do.
 
Preethi Chilukuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

I could not figure out how to include code tags.

Also,

My question:

There is a recursive call to printArray method. 


My assumption: As there are no {} around else block, only recursive call to printArray is executed.  System.out.println statement is executed only once.


My question:
How many times the following statement in the method printArray:

                                 System.out.println("[" + (i-1) + "] " + values[i-1]);


Logic and steps of execution please.  I am not able to understand this.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you compiled and ran this code, how many times did it print?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preethi Chilukuri wrote:
My assumption: As there are no {} around else block, only recursive call to printArray is executed.  System.out.println statement is executed only once.


Think about as the code returns out of each method call.
What happens when printArray(0) is called, and return occurs on line 9?
 
Preethi Chilukuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[0] 0
[1] 1
[2] 2
[3] 3
[4] 4
[5] 5
[6] 6
[7] 7
[8] 8
[9] 9

printed 9 times.  I could not get the logic how 9 times - System.out.println is executed... as else block do not have {} and i guess only printArray(i-1); is present in else blcok.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I say, think about what happens as each of the calls to printArray returns.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preethi Chilukuri wrote:. . . printed 9 times. . . .
That isn't nine times.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have given Dave's posts some "thumbs up" to indicate that you might want to pay attention to his posts the most.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For each call to that recursive method, if you pass a positive integer, it calls the method for one less (so passing 2 causes the method to be invoked later with 1). Then it prints the nth element of the array, remembering that in order to find the nth element of an array, you pass n − 1. So to find the second element when the argument is 2, it uses index 1.
Once you get below the first element of the array, the test for i==0 will stop the execution.

Because you call the method with n − 1 before you print array[n], you will get the array elements printed in their normal order.
After line 10, which is inside the else, line 11 is outside any particular control structures.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preethi Chilukuri wrote:My assumption: As there are no {} around else block, only recursive call to printArray is executed.  System.out.println statement is executed only once.

Question yourself first why there is no {} around else block while supposed to be. Fix that. So you'll have one assumption less to make.
 
Preethi Chilukuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for giving me hints to understand the recursive logic.

Got it!! 

Thanks all for your time.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think that way, beware: I got some weird imagination today.

Your scenario looks like that now. Imagine there is a cave. Outside the cave there are (9) fireworks on the ground, but you can't shoot them unless you go to the cave and then come back. So, when you do a recursive call, you go to the cave. Now that you are deeper in a cave, there you can also see fireworks on the ground, but 1 less this time (8), but you can't shoot them too unless you go deeper to the cave and then come back... imagine story continues till there is only 1 firework on the ground, and yet again you decide go deeper again, you go there, and you find out that there is nowhere else to go, so you have to come back. Now that you coming back, you were told you'll be able to shoot fireworks on the way back, so you start shooting them. So you shoot 1 (the ones you saw lately), then you go back further way out and you shoot 2, then 3, then 4, ... 9 which were apparently outside the cave. And you stop, end of printArray() body.

Please don't tell you didn't like my story
 
Preethi Chilukuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect story to understand recursive logic... for newbies !!



Thanks for your time for chipping in... 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!