Re: Is this recursion?



Marion wrote:
The recursive step is the "reversal(aWord,position-1)" call.

The base case is position equal to 0, which is handled differently from
non-zero. Specifically, the recursive step is not done in the base case.

They have the correct relation to each other, if position is initially
non-negative. The recursive step then uses a function of position,
position-1, that ensures that it must reach the base case, position==0.

Incidentally, you should look at your handling for empty strings.

Patricia

Thanks Patricia and Gordon,

I thought my base case was there but U wasn't confident about my reasoning until you two explained it a bit to me.

Patricia, I learned from Gordon in another program that strings were compared using ".equal" and not "==". I'm afraid I will never forget this even after I am 101 years old . . . with senility. It's been burned into my brain, permanently. :p)

True, but I don't see the relevance, because there were no string
compares in your code.

In the simplified example code I provided, the empty string does not exist since the program initialized before calling the recursive method. I understand that calling a recursive method when a negative number condition exists is not a happy event. In my current code that is not shown here, I do check for the empty string condition before proceeding.

Calling a recursive method with negative input does not have to be a
problem. The key condition is that the recursion process should always
reach a base case in a finite number of steps, when starting from any
permitted input.

Patricia
.



Relevant Pages

  • Re: Linking without -lpthread doesnt fail??
    ... I understand what you are saying but I don't ... > understand the purpose of such. ... Presumably you called pthread_create*before* calling ... In order to understand recursion you must first understand recursion. ...
    (comp.programming.threads)
  • Re: indirect recursion
    ... Any recursion can be hard to debug. ... like A calling B twice, and B calling C twice, and C calling A twice, that ...
    (comp.lang.c)
  • Re: Got stuck with redirecting to less
    ... >>Sure it does, on exit. ... > But it was calling closeon the FD before it exited. ... So when stdio tried ... In order to understand recursion you must first understand recursion. ...
    (comp.unix.programmer)
  • Re: Increasing stack size
    ... > I had to code a algorithm using recursion. ... > throws StackOverFlow error. ... > How can I increase stack size in JDK 1.4? ... You probably have an error in your recursive method. ...
    (comp.lang.java.programmer)
  • Re: Increasing the stack?
    ... then it pops the stack before the jump, ... > in time between the iterator and recursive method. ... > also possible to do it without a test at each recursion. ... > java is doing a test, or just sitting back and waiting to be told to do ...
    (comp.lang.java.help)