Chat LIVE With Programming Experts! There Are 23 Online Right Now...

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

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




recursive function

 
Reply to this topicStart new topic

recursive function, calculate the final value of An

Tzung-Shian
4 Jan, 2009 - 10:47 PM
Post #1

New D.I.C Head
*

Joined: 19 Dec, 2008
Posts: 20

/* sorry i surely have no any idea, so please forgive me

i can't post any my code

the question is to calculate the final value of an, using

recursive function

an = an-1 + an-3
a0 = 1
a1 = 2
a2 = 0

*/

User is offlineProfile CardPM
+Quote Post


AmitTheInfinity
RE: Recursive Function
4 Jan, 2009 - 10:58 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,258



Thanked: 64 times
Dream Kudos: 125
My Contributions
I hope you have already read this.

Well, your problem description itself looks like a code, if you look at it properly.
QUOTE

int func(int n)
{
check if n = 0 if yes return value 1
check if n = 1 if yes return value 2
check if n = 2 if yes return value 0
return value func(n-1) + func(n-3) //this will be your recursive call
}


I hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

Tzung-Shian
RE: Recursive Function
4 Jan, 2009 - 11:18 PM
Post #3

New D.I.C Head
*

Joined: 19 Dec, 2008
Posts: 20

/* this is my code, but still has some problem

thank u for ure instruction

*/

#include <stdio.h>

int func(int n);
int func(int n)
{

if (n = 0)
{
return 1;
}
if (n = 1)
{

return 2;
}
if (n = 2)
{
return 0;
}
else{
return func(n-1) + func(n-3);
} //this will be your recursive call
}





int main()
{
int n;
printf("%d",func(n-1) + func(n-3));




return 0;

}
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Recursive Function
4 Jan, 2009 - 11:23 PM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,258



Thanked: 64 times
Dream Kudos: 125
My Contributions
Well, = is an assignment operator. and == is comparison operator. you should use == in if.

While calling from n, you forgot to give initial value to n, assign that value to n and your code will start working.
User is offlineProfile CardPM
+Quote Post

Tzung-Shian
RE: Recursive Function
4 Jan, 2009 - 11:34 PM
Post #5

New D.I.C Head
*

Joined: 19 Dec, 2008
Posts: 20

/* it still cannot compile

why......

*/

#include <stdio.h>

int func(int n);
int func(int n)
{

if (n == 0)
{
return 1;
}
if (n == 1)
{

return 2;
}
if (n == 2)
{
return 0;
}
else{
return func(n-1) + func(n-3);
} //this will be your recursive call
}





int main()
{
int n=1;

printf("%d",func(n));




return 0;

}
User is offlineProfile CardPM
+Quote Post

Tzung-Shian
RE: Recursive Function
4 Jan, 2009 - 11:50 PM
Post #6

New D.I.C Head
*

Joined: 19 Dec, 2008
Posts: 20

I find out !!!

thank u

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 05:54PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month