Could anyone afford something on recursion ? I have test Hanoi code but still feel it is not enought for comprehension because of my poor knowledge on Math.

thanx in advanced.

Printable View

- 07-03-2002blackMmm...recursion~
Could anyone afford something on recursion ? I have test Hanoi code but still feel it is not enought for comprehension because of my poor knowledge on Math.

thanx in advanced. - 07-04-2002|deep|
You should go and learn some data structures(especially linked lists and binary trees) to practice a lot of recursion problems.

Some very good problems are in Stanford computer science library online. They are both introductory and challenging to some extent.

Then you can go further and learn about game theory and compression. Ask me if you can't have enough information and links. - 07-04-2002blackQuote:

*Originally posted by |deep|*

**You should go and learn some data structures(especially linked lists and binary trees) to practice a lot of recursion problems.**

Some very good problems are in Stanford computer science library online. They are both introductory and challenging to some extent.

Then you can go further and learn about game theory and compression. Ask me if you can't have enough information and links.

Would u mind supporting me anything which would help me to understand recursion utterly ? - 07-04-2002Unregistered
recursion means calling the same function within the function:

Code:`bool function(int data)`

{

//whatever

function(newData);//recursive call of function()

//whatever

}

Code:`bool function(int data)`

{

//terminating condition

//do something different if terminating condition exists

//otherwise continue blithely onward

function(newData);

//whatever

}

Code:`bool function(int data)`

{

if(data == 10)

{

cout << data << endl; //print out data

return false;

}

/*if data equals 10 this will stop this call and return false to the function that made this call. The calling function ignores the return, but that doesn't matter, and it won't be ignored in all recursive functions. IF data equals 10 none of the stuff below will be used. If data != 10 then function() will proceed to stuff below

*/

cout << data << endl; //here we print out the desired info

int newData = ++data;

/*here we increment data and assign it to newData. You could pass ++data itself but I think this is clearer

*/

function(newData); //here's the recursive call

return false;

/*this return won't happen untile the line immediately above it is completed if data isn't 10.

*/

}

Code:`#include <iostream.h>`

bool function(int data);

int main()

{

int Data = 0;

function(Data);

return 0;

}

bool function(int data)

{

if(data == 10)

{

cout << data << endl;

return false;

}

cout << data << endl;

int newData = ++data;

function(newData);

return false;

}

Code:`bool function(int data)`

{

if(data == 10)

{

cout << data << endl;

return false;

}

//cout << data << endl;

int newData = ++data;

function(newData);

cout << data << endl;

return false;

}

- 07-04-2002Shiro
Another famous example, the factorial function:

n! = n x (n-1) x (n-2) x .. x 3 x 2 x 1

This is a recursive definition. The is reached when 1 is reached, which is the end-step of the recursion. The recursion step is n x n-1)!. This can be easily seen as follows:

2! = 2 x 1

3! = 3 x 2 x 1 = 3 x 2!

4! = 4 x 3 x 2 x 1 = 4 x 3!

n! = n x (n-1) x .. x 2 x 1 = n x (n-1)!

This leads to this recursive function:

Code:`int fac (int n)`

{

if (n == 1) /* the end-step */

return n;

else

return (n * fac (n - 1));

F[0] = 0

F[1] = 1

F[n] = F[n-1] + F[n-2]

Here the end-step is reached if n = 1. The recursion step is the recursive relation F[n] = F[n-1] + F[n-2]. This leads to the recursive function:

Code:`int fib (int n)`

{

if (n == 1)

return (n);

else

return (fib (n-1) + fib (n-2));

}

http://mathworld.wolfram.com/FibonacciNumber.html - 07-05-2002black
thanx guys.

I am a bit confused 1 second ago and now clearly with all your help~ :D

In fact, I had written some functions with recursion already. I will paste them here for sharing. ;) - 07-05-2002black
//these functions below are all with recursion method:

PHP Code:`//function 1;`

int Fact(int n)

{

if(n==0)

{

return 1;

}

else if(n>0)

{

return n*Fact(n-1);

}

}

//function 2;

int Fib(int n)

if(n==1 || n==2)

{

return 1;

}

else if(n>2)

{

return Fib(n-1)+Fib(n-2);

}

}