Solved

a  problem of function recursion

Posted on 2002-07-11
6
185 Views
Last Modified: 2010-04-15
I'm primer, I didn't know well about executive process of function recursion .for example

/* a recursive function instance */
int RecuFun(int n)
{
    int f1,f2;
     
    if ( n ==1 || n == 0 )
            return 1;
    else {
            f1 = RecuFun(n - 1);
            f2 = RecuFun(n - 2);
             
            return f1 + f2;

     }
}

main()
{
     int num =  5;
     int result;

     result = RecuFun( num );
     printf("%d\n",result);
}


running result :
8

but i am confused in else sentence section. please give a explanation thanks :)
0
Comment
Question by:China_Ark
6 Comments
 
LVL 12

Accepted Solution

by:
pjknibbs earned 50 total points
Comment Utility
I'm not sure what your difficulty is. Basically, the code in the "ELSE" section simply calls the recursive function again with different values--the IF statement is there to make sure the recursion ends at some point, because if the function ALWAYS called itself you'd get into an infinite loop and would crash with a stack space error.

Basically, the above code calls RecuFun(5). RecuFun(5) returns the value of RecuFun(4) + RecuFun(3). RecuFun(3) returns the value of RecuFun(2) + RecuFun(1). RecuFun(1) returns 1, ending the recursion down that branch. (Note I've missed one of the recursive calls at each level here).

It's perhaps easier to understand if you only have one recursive call in the function, e.g.

int factorial(int n)
{
  if (n == 1)
    return 1;
  else
    return n * factorial(n - 1);
}

This function calculates a factorial (e.g. factorial(6) = 6*5*4*3*2*1).
0
 
LVL 3

Expert Comment

by:cincin77
Comment Utility
an implementation of fibonacci numbers
what exactly did not you understand?
do you know about fibonacci numbers?
0
 

Author Comment

by:China_Ark
Comment Utility
thanks to pjknibbs and cincin77.

Actually,it 's fibonacci function,the blow is its revise.
but still not efficent.
long RecuFun(int n)
{
        long f1,f2;
        if ( n ==1 || n == 0 )
            return 1;
        else {
        f1 = RecuFun(n - 1);
        f2 = RecuFun(n - 2);

        return f1 + f2;
        }
}

main()
{
        int num;
        long result;

        for( num = 0;num <=40;num++) {
            result = RecuFun( num );          
              printf("%10ld",result);
            if ( (num + 1) % 4 == 0 )
                    printf("\n");
        }
}
I don't see that as pjknibbs say, the main functin calls
RecuFun(5).
RecuFun(5) = RecuFun(4) + RecuFun(3)
_______________|            |
|  ________________________|
| |  
| |__> RecuFun(3) = RecuFun(2) + RecuFun(1);
|       ______________|
|       |__> RecuFun(2) = RecuFun(1) + RecuFun(0).
|____> RecuFun(4) = RecuFun(3) + RecuFun(2);    
     _________________|            |
     |  __________________________|
     |  |__> RecuFun(2) = RecuFun(1) + RecuFun(0);        
     |_____> RecuFun(3) = RecuFun(2)+ RecuFun(1);
                 ____________|      
                |__>RecuFun(2) = RecuFun(1) + RecuFun(0);
     
RecuFun(1) returns 1, ending the recursion
down that branch.

all right? :) ok. thanks to pjknibbs and cincin77 for giving me some good word.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:China_Ark
Comment Utility
thanks to pjknibbs and cincin77.

Actually,it 's fibonacci function,the blow is its revise.
but still not efficent.
long RecuFun(int n)
{
        long f1,f2;
        if ( n ==1 || n == 0 )
            return 1;
        else {
        f1 = RecuFun(n - 1);
        f2 = RecuFun(n - 2);

        return f1 + f2;
        }
}

main()
{
        int num;
        long result;

        for( num = 0;num <=40;num++) {
            result = RecuFun( num );          
              printf("%10ld",result);
            if ( (num + 1) % 4 == 0 )
                    printf("\n");
        }
}
I don't see that as pjknibbs say, the main functin calls
RecuFun(5).
RecuFun(5) = RecuFun(4) + RecuFun(3)
_______________|       |
|  ________________________|
| |  
| |__> RecuFun(3) = RecuFun(2) + RecuFun(1);
|       ______________|
|       |__> RecuFun(2) = RecuFun(1) + RecuFun(0).
|____> RecuFun(4) = RecuFun(3) + RecuFun(2);    
     _________________|       |
     |  __________________________|
     |  |__> RecuFun(2) = RecuFun(1) + RecuFun(0);        
     |_____> RecuFun(3) = RecuFun(2)+ RecuFun(1);
                 ____________|      
                |__>RecuFun(2) = RecuFun(1) + RecuFun(0);
     
RecuFun(1) & ReuFun(0) returns 1, ending the recursion
down that branch.

all right? :) ok. thanks to pjknibbs and cincin77 for giving me some good word.
0
 
LVL 2

Expert Comment

by:jonnin
Comment Utility
recursion is not generally efficient. The extra function calls cost more if nothing else does, and there is danger of blowing the stack on larger problems. Write it iteratively, if that is too slow create a lookup table of the numbers (1 operation to get a value, no loops or computations). Of course you will have to make the table large enough for whatever problem you are solving, with notes on when the table runs out...



(you can use the output of the slow function to create the values for the table, since you have it)

to speed it up as it is, store in a static array variable the computations you have already done (cache) and get them instead of recomputing ...

0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept pjknibbs' comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now