954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help in Recursion

I need to understand the folowing code

[code]return(fab(n-1)+fab(n-2));[\code]

As most of u will be fimiliar with this itz to make fabonachi series in C but can any one tell me How the computer Reads it i mean how is the sontrol transfered plzzz

Ali Shahzad
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

I need to understand the folowing code

[code]return(fab(n-1)+fab(n-2));[\code]

As most of u will be fimiliar with this itz to make fabonachi series in C but can any one tell me How the computer Reads it i mean how is the sontrol transfered plzzz

What do you mean???

The above code returns sum of fab()'s return value for n - 1 and n - 2...

FoX_
Junior Poster in Training
53 posts since Mar 2007
Reputation Points: 10
Solved Threads: 7
 

by the way... the correct way to wrap your code is [code] [/code]

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

this looks like a simple recursive function, and that piece of code is the part where it returns the next value of the fibonacci series... i suppose before that there is a "cout<<" that calls the recursive function?

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

>but can any one tell me How the computer Reads it i mean how is the sontrol transfered plzzz
Do a google search for "recursive fibonacci trace". Most of the pages will have at least one image showing you transfer of control within the recursive path.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
can any one tell me How the computer Reads it i mean how is the sontrol transfered plzzz

I think it would be better 4 you if you first learned how to use recursive functions b4 you tried one yourself... or is this some sort of benchmarking?

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

You had better look for the explanation in books, or internet. This recursive function you talk about is quite common in programming. So it will be definitely there in any programming book. You'll find a neat and nifty explanation there.

By the way, it's fibonacci, not fabonachi.

wendy2learn
Newbie Poster
10 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Thanx all for concern

I will try to understand it Through Google


Thnx Again

Ali Shahzad
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Well I have made the Program but there is a problem What ever value i enter i answers 0

Can any one point out The problem in COde

#include<stdio.h>
#include<conio.h>
int fab (int num);
void main ()
{
int num;
clrscr();
printf("Please enter the no yo which You want Fabbnicci series");
scanf("%d",&num);
printf("%d",fab(num));
getch();
}
int fab (int num)
{
if(num==0 | num==1)
return(0);
else
return(fab(num-1)+fab(num-2));
}
Ali Shahzad
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Close. If num is 0 you return 0, but if num is 1 you return 1, not 0. Also, | is not the same as ||. The former is a bitwise OR and the latter is a logical OR:

int fab ( int num )
{
  if ( num==0 || num==1 )
    return num;
  else
    return fab ( num - 1 ) + fab ( num - 2 );
}

>#include
You don't need this and it destroys the portability of your code. Get rid of it until you know how to use it properly.

>void main ()
This is never correct. The correct definition of main is:

int main ( void ) {
  /* Your code here */
  return 0;
}

>clrscr();
This is unnecessary when working from an IDE and extremely antisocial when working from the command line. In other words, get rid of it until you know how to use it properly.

>printf("Please enter the no yo which You want Fabbnicci series");
It's Fibonacci, not Fabbnicci. You've been told this already. Know how to spell the things you use because you can't do effective research otherwise.

>scanf("%d",&num);
Always error check your input.

>getch();
This is unnecessary when working from the command line and can easily be replaced with a standard solution when working from an IDE. Use getchar instead, it's portable at least.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I didnt Understand can u plzz explain why it prints 0

Ali Shahzad
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

>can u plzz explain why it prints 0
What's 0 + 0? That's why it prints 0. :icon_rolleyes:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Can u please Correct it I have tried but cant do it

Ali Shahzad
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 
Also, | is not the same as ||. The former is a bitwise OR and the latter is a logical OR.


Isn't it also a logical OR without the short circuit evaluation?

#include<stdio.h>

int first()
{
    printf("\nIn first function");
    return 1;
}

int second()
{
    printf("\nIn second function");
    return 0;
}

int main(void)
{
    if(first() | second())
    {
        printf("\nWithout short circuit evaluation");
    }
    putchar('\n');
    if(first() || second())
    {
        printf("\nWith short circuit evalutaion");
    }
    getchar();
    return 0;
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

>Can u please Correct it I have tried but cant do it
Now I don't understand. I gave you the corrected function. What more do you want?

>Isn't it also a logical OR without the short circuit evaluation?
It can be used as such, provided you're careful. It's generally a bad idea to use the bitwise operators for boolean logic. There can be surprising results.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I have made some changes But still it does not generate the required results it printf only one number not the series

#include<stdio.h>
int fab (int num);
int main (void)
{
int num;
clrscr();
printf("Please enter the no yo which You want Fibonacci series");
scanf("%d",&num);
printf("%d",fab(num));
return(0);
}
int fab (int num)
{
if(num==0 || num==1)
return num;
else
return fab(num-1)+fab(num-2);
}
Ali Shahzad
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

>But still it does not generate the required results it printf only one number not the series
That's because you only ask for one number. fab gives you the ith number in the series, not the entire series. If you want the entire series, you need to call fab in a loop:

int i;

for ( i = 0; i < 10; i++ )
  printf ( "%d ", fab ( i ) );
printf ( "\n" );

And you need to start formatting your code. Also, clrscr is declared in conio.h. Your code shouldn't compile until you remove it.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thnx Dear

U really helped me Thnx again

NOw i just have to Understand it correctly Again LOlz

Can u plzz Suggest a book for begginers in C

Ali Shahzad
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

The best beginner's book I've found for C is "Pointers on C" by Kenneth Reek.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You