Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,402 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,120 people online right now. Registration is fast and FREE... Join Now!




recursive program

 
Reply to this topicStart new topic

recursive program

sknox1
17 Oct, 2006 - 07:59 AM
Post #1

New D.I.C Head
Group Icon

Joined: 30 Sep, 2006
Posts: 29


Dream Kudos: 150
My Contributions
can anybody help me implement the following?

CODE

f(a,B)=a-b
if a=0 or b=0
f(a-1,B) +f(a,b-1) else

huh.gif
User is offlineProfile CardPM
+Quote Post

Nova Dragoon
RE: Recursive Program
17 Oct, 2006 - 08:29 AM
Post #2

The Innocent Shall Suffer, Big Time
Group Icon

Joined: 16 Aug, 2001
Posts: 6,132



Thanked: 7 times
Dream Kudos: 515
Expert In: Python, Linux

My Contributions
Can you show us what you have done so far in the way of code?
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Recursive Program
17 Oct, 2006 - 09:46 AM
Post #3

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
QUOTE(Trogdor @ 17 Oct, 2006 - 09:30 AM) *





Is there really any need for that? It's with comments like that, that people get scared of coming to forums for fear of being riduculed. These places are supposed to be frendly and helpful and you should encourage them to stay. And i would say three lines of code is very readable wouldn't you?

Edit by Nova Dragoon:
User is offlineProfile CardPM
+Quote Post

sknox1
RE: Recursive Program
17 Oct, 2006 - 11:26 AM
Post #4

New D.I.C Head
Group Icon

Joined: 30 Sep, 2006
Posts: 29


Dream Kudos: 150
My Contributions
QUOTE(sknox1 @ 17 Oct, 2006 - 08:59 AM) *

can anybody help me implement the following?

CODE

f(a,B)=a-b
if a=0 or b=0
f(a-1,B) +f(a,b-1) else

huh.gif

I meant in to a recursive program
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Recursive Program
17 Oct, 2006 - 12:42 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You may have to provide some more information...are these formulae you were given? Part of a program?
User is offlineProfile CardPM
+Quote Post

sknox1
RE: Recursive Program
22 Oct, 2006 - 10:18 AM
Post #6

New D.I.C Head
Group Icon

Joined: 30 Sep, 2006
Posts: 29


Dream Kudos: 150
My Contributions
QUOTE(Nova Dragoon @ 17 Oct, 2006 - 09:29 AM) *

Can you show us what you have done so far in the way of code?

here is the finished program.
CODE
int fib(int a,int B)

{
    if (a==0 || b==0)
       return a-b;

     else
    
    return   fib(a-1,B) + fib(a,b-1);

}


int main()



{
     int number1;
     int number2;


     cout<<"Please enter two numbers "<< " " <<endl;
     cin>>number1;
     cin>>number2;    
     cout << fib(number1, number2);

     return 0;
}

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Recursive Program
23 Oct, 2006 - 03:53 AM
Post #7

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
This recursive function calculates the next Number in the Fibonacci Series.
To display the complete series, you'll need to place the Function Call in a loop and pass the return value as the 2nd argument and number2 as the 1st argument to the fib() function.

What can't you understand in the code? Are you confused in trying to figure out how it works?

Let us know by posting here smile.gif
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Recursive Program
23 Oct, 2006 - 04:12 AM
Post #8

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 533



Thanked: 3 times
Dream Kudos: 125
My Contributions
Looks like you implemented it.
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Recursive Program
23 Oct, 2006 - 04:14 AM
Post #9

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(sknox1 @ 22 Oct, 2006 - 11:18 AM) *


here is the finished program.
CODE
int fib(int a,int B)
{
    if (a==0 || b==0)
       return a-b;

     else
    
    return   fib(a-1,B) + fib(a,b-1);

}


Perhaps you like to read here on recursion.

My take on this is:
return fib(a-1,cool.gif + fib(a,b-1);

will call up the fib function for all the values of a-1, then redo that for all the functions of b-1...

which doesn't seem to work correctly... have you confirmed you get a sequence like 1,1,2,3,5,8...

This post has been edited by gregoryH: 23 Oct, 2006 - 04:17 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month