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.

This is a discussion on *Mmm...recursion~* within the **C++ Programming** forums, part of the General Programming Boards category; Could anyone afford something on recursion ? I have test Hanoi code but still feel it is not enought for ...

- 07-04-2002 #1
## Mmm...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.Never end on learning~

- 07-04-2002 #2

- Join Date
- May 2002
- Posts
- 21

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-2002 #3
*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 ?Last edited by black; 07-04-2002 at 04:12 AM.

Never end on learning~

- 07-04-2002 #4UnregisteredGuest
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-2002 #5

- Join Date
- Aug 2001
- Location
- Groningen (NL)
- Posts
- 2,386

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-2002 #6
thanx guys.

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

In fact, I had written some functions with recursion already. I will paste them here for sharing.Never end on learning~

- 07-05-2002 #7
//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);

}

}

Never end on learning~

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011