Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

recrusion  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


program shows compile error due to recrusive calls can you explain the reason?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



......

program shows compile error due to recrusive calls can you explain the reason?


Let us first improve how the get method looks, so we can understand it better. Given below is a formatted version of your get method.



See, now it looks much easier to analyse.

Now what happens if you substitute 6 in the place of a?



 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But did you say a compiler error? I am not getting any compiler error.

So how are you compiling the program? Could you paste the compiler error here.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is the program i had compiled getting runtime error?
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And now you tell us why you are getting a stack overflow error. Hint: start with this FAQ.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you tell me how i can avoid this ?
 
Henry Wong
author
Sheriff
Posts: 22821
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:can you tell me how i can avoid this ?


Your program in the original post, has an endless recursion call -- it calls itself forever, until the stack overflows.

As for "how to avoid this", we have absolutely no idea of what you are trying to do, so the only answer is "don't do that".

Henry

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above the program why the program throws stackoverflow error during runtime as during the second recursive call the value of 'a' becomes 5 the condition fails no error !!
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For debugging purposes, there's nothing that beats a good old println.
So try:

And try to explain what you get.

Greetz,
Piet
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why this program throws stackover flow exception?
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first: did you manage to solve your opening post question?
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i solved it.can you answer for above post?
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I was actually asking for an explanation. But allright, for this time.

Well, you are splitting the array a[] into two parts, and you might be forgiven
to think that after the first call s[] to be {1, 3, 2}. But according to your
'println' it is not. Why is that? Hint: print s[] as well.

Another hint: If you were to use ArrayLists in stead of arrays,
you would not get an overflow.

Greetz,
Piet
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell me in the second round j value fails the condition but it shows stackoverflow error?
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand the question.
It would help if you posted code with consistent indentation and spacing, so we can actually read it.
 
Henry Wong
author
Sheriff
Posts: 22821
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:please tell me in the second round j value fails the condition but it shows stackoverflow error?


Don't know what you are doing in that mess (prior to the recursive call) ... but whatever it is, it is not working... your latest program has an endless recursive call.

[EDIT: okay, I see what you are doing. And as mentioned, the J value is not working to stop your recursion. As for why... hint ... what happens when you don't set an element in an array?

BTW, Piet's hint works well too, but I didn't understand it until I understood what was going on... ]

Henry
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Admittedly: my first reaction was also: "What the heck...".
And I had to read it a couple of times too, before I knew what
was going on. This Bairava... never a dull question

Greetz,
Piet
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just need to know the reason for stackoverflow error!
my program logic is making recursive call if my j value is greater than 2.
during first call j value is 3 (s->1,3,2)
during the second call j value is 0 then y it is showing error?
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Bairava,

do you have a computer on which you run your programs? If so,
what brand is it?

When I run your code, I get the following first 9 outcomes for j:

3, 7, 9, 9, 9, 9, 9, 9, 9

so what on earth are you talking about?

 
Henry Wong
author
Sheriff
Posts: 22821
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:
during first call j value is 3 (s->1,3,2)


Correct. During the first call, you found three numbers that is less than the first value (of 5), so "j" equals 3.

However, you are wrong regarding "s".

s -> 1, 3, 2, 0, 0, 0, 0, 0, 0, 0

remember that you created an array of size 10 to return the result -- so s refers to a result with 10 values.

bairava surya wrote:
during the second call j value is 0 then y it is showing error?


Incorrect. At this point, "j" should be zero, but the application is reporting seven -- and for some reason, you are choosing to ignore the debugging messages that are printed. Why?

Anyway, why is it reporting seven? It is reporting seven because there are seven zeros in "s" that are less than one.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!