English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

5 answers

http://www.imada.sdu.dk/~svalle/courses/dm14-2005/mirror/c/subsection3_4_6.html#SECTION0004600000000000000

2006-12-21 20:56:40 · answer #1 · answered by jan 7 · 2 0

abhay

For example, here's a simple rule for generating the triangular numbers:

def tri(n):
# triangular num n
# = sum of n consecutive counting nos (n>=1)
if n<=1: return n
else: return n + tri(n-1)

A canonical example of recursion is the computation of Fibonacci numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946 ...

This sequence can be defined recursively as follows (F(n) is the nth Fibonacci number):

* if n = 0, F(n)= 0
* if n = 1, F(n) = 1
* otherwise, F(n) = F(n - 1) + F(n - 2)

You can easily follow this definition and generate some Fibonacci numbers for yourself - you will get precisely the sequence given above.

Here is the C++ code that returns any Fibonacci number (constrained by execution-time and limitations of the C++ long type):

long fib_rec( long index )
{
if (index < 2)
return index;
else
return fib_rec(index - 1) + fib_rec(index - 2);
}

try reading this and learn

http://isg.cs.tcd.ie/giangt/recursion.pdf
http://theory.cs.uvic.ca/amof/e_fiboI.htm
http://www.math.sunysb.edu/~scott/Book331/Recursion_making_Koch.html
http://www.thescripts.com/forum/thread565938.html

2006-12-22 05:32:38 · answer #2 · answered by Joe_Young 6 · 0 0

And why exactly a recursive function? Why not a couple of simple loops instead?
Are you posting all your homework questions here, dear? :-)

2006-12-22 05:28:08 · answer #3 · answered by Mandar M 1 · 0 0

// to generate fibonacci series using recursive function
void main()
{
int fib(int);
inta=0,temp,n;
cout< cout< cin>>n;
for(i=1,i<=n-1,i++)
{temp=fib(i);
cout< }
getch();
}
int fib (intn)
{if(m==1||m==2)
return 1;
else
return fib (m-1)+fib(m-2);
}




if you have any problum regarding c++ contact me on rishi_umesh2005@yahoo.co.in

2006-12-22 07:50:27 · answer #4 · answered by rishi_325 1 · 1 0

wat the hell u r asking, very easy one.
u can't do it?
check out book dude.

2006-12-22 10:22:37 · answer #5 · answered by na k 1 · 0 0

fedest.com, questions and answers