I am bit confused about the recuersive function details.

Pls see the code below it's pretty simple.

And here is my understanding of what's happening inside but I know it's wrong. Could anyone point where my understanding went wrong?

If...

sum(3) : Goes into the recursive function

with the parameter of sum(3-1 = 2)

sum(2) : Goes into the recursive function

with the parameter of sum(2-1 = 1)

sum(1) : Goes into the if(n==1) and return 1

After the return 1, the function recurse.

[red]Question:Does [b]1[/b] of [b]return 1[/b] have any numeric value?[/red]

[b]Recursion:[/b]

n = 1

sum(n-1) = 0

[b][red]n + (n-1) = 0[/b][/red]

n = 2

sum(n-1) = 1

[b][red]n + (n-1) = 3[/b][/red]

n = 3

sum(n-1) = 2

[b][red]n + (n-1) = 5[/b][/red]

This logic should NOT output [b]6[/b] as an answer but it does, so my understanding above should be incorrect.

Could anyone point me out please?

Thank you!!

Here is the code & output;

[code]

#include

int sum(int n) {

if (n == 0)

return 0;

if (n == 1)

return 1;

else

return n + sum(n - 1);

}

void main() {

int result;

result = sum(3);

printf("sum(3) is %d

", result);

result = sum(10);

printf("sum(10) is %d

", result);

getchar();

}

[/code]

[b]OUTPUT[/b]

Sum(3) is 6

Sum(10) is 55

It looks like you're new here. If you want to get involved, click one of these buttons!

- 140.8K All Categories
- 103.6K Programming Languages
- 6.4K Assembler Developer
- 401 Assembly Code Share
- 239 Getting started in assembly
- 4.6K x86 Assembly
- 1.9K Basic
- 97 Qbasic
- 39.9K C and C++
- 5.6K Beginner C/C++
- 330 C/C++ on Linux/Unix
- 450 C/C++ Windows API
- 522 C++ Builder
- 253 C++ Game Development
- 3.3K C++ MFC
- 103 C++.NET
- 404 Visual C++
- 2.9K C#
- 7.9K Delphi and Kylix
- 334 Advanced Delphi
- 360 Delphi beginners
- 4 Haskell
- 9.7K Java
- 56 Enterprise JavaBeans
- 1.3K Java Beginners
- 304 Java Server Pages
- 4.1K Pascal
- 1.3K Perl
- 11 Perl 6
- 2K PHP
- 546 Python
- 37 Ruby
- 4.4K VB.NET
- 258 Advanced VB.Net
- 1.6K VBA
- 20.8K Visual Basic
- 767 Access databases and VB
- 831 Advance Visual Basic
- 1.2K Beginner VB
- 2.6K Game programming
- 315 Console programming
- 90 DirectX Game dev
- 1 Minecraft
- 112 Newbie Game Programmers
- 2 Oculus Rift
- 9K Applications
- 1.8K Computer Graphics
- 279 3D Graphics
- 129 DirectX
- 125 OpenGL
- 740 Computer Hardware
- 9 Cooling & Overclocking
- 3.4K Database & SQL
- 1.1K Access
- 91 ADO Programming
- 288 MySQL
- 358 Oracle
- 440 SQL-Server
- 535 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 25 DirectSound
- 257 XML Development
- 3.3K Classifieds
- 199 Co-operative Projects
- 198 For sale
- 190 FreeLance Software City
- 1.9K Jobs Available
- 603 Jobs Wanted
- 209 Wanted
- 2.9K Microsoft .NET
- 1.8K ASP.NET
- 1.1K .NET General
- 22 .NET WEB-Services
- 129 .NET WinForms
- 14 .NET XML
- 50 ADO.NET
- 142 C# & VB.NET School Support
- 3.4K Miscellaneous
- 8 Join the Team
- 354 Comments on this site
- 69 Computer Emulators
- 2.1K General programming
- 187 New programming languages
- 621 Off topic board
- 200 Mobile & Wireless
- 72 Android
- 126 Palm Pilot
- 338 Multimedia
- 154 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 27 Cloud Computing
- 1 Witsbits Go Cloud
- 53 FreeBSD
- 1.7K LINUX programming
- 1 Awk scripting
- 332 Linux Support
- 0 Sed scripting
- 370 MS-DOS
- 0 Shell scripting
- 321 Windows CE & Pocket PC
- 4.1K Windows programming
- 177 COM/DCOM
- 61 Networking And Security
- 17 Windows 2003 Server
- 6 Windows Vista
- 176 Windows XP
- 939 Software Development
- 416 Algorithms
- 68 Object Orientation
- 24 RUP & UML
- 91 Project Management
- 95 Quality & Testing
- 268 Security
- 63 Evil Scripting
- 81 Hacking
- 7.7K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 4 Bootstrap Themes
- 55 CGI Development
- 28 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 131 Mobile Internet & Messaging
- 211 Wireless development
- 2.2K JavaScript
- 37 JQuery
- 304 WEB Servers
- 153 Apache
- 79 IIS
- 150 WEB-Services / SOAP

## Comments

:

: I am bit confused about the recuersive function details.

: Pls see the code below it's pretty simple.

: And here is my understanding of what's happening inside but I know it's wrong. Could anyone point where my understanding went wrong?

:

: If...

: sum(3) : Goes into the recursive function

: with the parameter of sum(3-1 = 2)

: sum(2) : Goes into the recursive function

: with the parameter of sum(2-1 = 1)

: sum(1) : Goes into the if(n==1) and return 1

:

: After the return 1, the function recurse.

:

: [red]Question:Does [b]1[/b] of [b]return 1[/b] have any numeric value?[/red]

:

: [b]Recursion:[/b]

: n = 1

: sum(n-1) = 0

: [b][red]n + (n-1) = 0[/b][/red]

:

: n = 2

: sum(n-1) = 1

: [b][red]n + (n-1) = 3[/b][/red]

:

: n = 3

: sum(n-1) = 2

: [b][red]n + (n-1) = 5[/b][/red]

:

:

: This logic should NOT output [b]6[/b] as an answer but it does, so my understanding above should be incorrect.

:

: Could anyone point me out please?

: Thank you!!

:

: Here is the code & output;

: [code]

: #include

:

: int sum(int n) {

: if (n == 0)

: return 0;

: if (n == 1)

: return 1;

: else

: return n + sum(n - 1);

:

: }

:

: void main() {

: int result;

:

: result = sum(3);

: printf("sum(3) is %d

", result);

:

: result = sum(10);

: printf("sum(10) is %d

", result);

:

: getchar();

: }

: [/code]

:

: [b]OUTPUT[/b]

: Sum(3) is 6

: Sum(10) is 55

:

[purple]

Hey tokoG, u r almost got it :-)

here i am explaining again sum(3) in my way:

[code][size=4]

sum(3)

n = 3

return 3 + [red]sum(3-1)[/red]

sum(2)

n = 2

return 2 + [green]sum(2-1)[/green]

sum(1)

n = 1

return 1

return 2 + [green]1[/green]

return 3 + [red]3[/red]

[/size][/code]

and the last return statement exits from the call sum(3) returning 6.

[/purple]

[hr][purple]~Donotalo()[/purple]

: [purple]

: Hey tokoG, u r almost got it :-)

: here i am explaining again sum(3) in my way:

: [code][size=4]

: sum(3)

: n = 3

: return 3 + [red]sum(3-1)[/red]

: sum(2)

: n = 2

: return 2 + [green]sum(2-1)[/green]

: sum(1)

: n = 1

: return 1

: return 2 + [green]1[/green]

: return 3 + [red]3[/red]

: [/size][/code]

: and the last return statement exits from the call sum(3) returning 6.

: [/purple]

: [hr][purple]~Donotalo()[/purple]

:

:

Hi Donotalo, how are you?

Thanks for the reply, does it mean when...

[code]

: sum(1)

: n = 1

: return 1

[/code]

The n = becomes 2 [(n=1) + (return 1) = 2]at the sentence...

[code]

return n(2) + [green](n-1)1[/green]

[/code]

is it?

: : [purple]

: : Hey tokoG, u r almost got it :-)

: : here i am explaining again sum(3) in my way:

: : [code][size=4]

: : sum(3)

: : n = 3

: : return 3 + [red]sum(3-1)[/red]

: : sum(2)

: : n = 2

: : return 2 + [green]sum(2-1)[/green]

: : sum(1)

: : n = 1

: : return 1

: : return 2 + [green]1[/green]

: : return 3 + [red]3[/red]

: : [/size][/code]

: : and the last return statement exits from the call sum(3) returning 6.

: : [/purple]

: : [hr][purple]~Donotalo()[/purple]

: :

: :

:

: Hi Donotalo, how are you?

:

: Thanks for the reply, does it mean when...

:

: [code]

: : sum(1)

: : n = 1

: : return 1

: [/code]

:

: The n = becomes 2 [(n=1) + (return 1) = 2]at the sentence...

:

: [code]

: return n(2) + [green](n-1)1[/green]

: [/code]

:

: is it?

:

[purple]hey, i'm fine. :-)

probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.

well, if sum(2) [leftbr]n = 2[rightbr] has been called, it will return [blue]n + sum(1)[/blue]. but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).

when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.

sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).

now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.

i'm sleepy, so there might be mistakes in my explanation. forgive me for that ;-)

[/purple]

[hr][purple]~Donotalo()[/purple]

:

: probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.

:

: well, if sum(2) [leftbr]n = 2[rightbr] has been called, it will return [blue]n + sum(1)[/blue]. but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).

:

: when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.

:

: sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).

:

: now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.

:

: i'm sleepy, so there might be mistakes in my explanation. forgive me for that ;-)

: [/purple]

: [hr][purple]~Donotalo()[/purple]

:

:

Hi Donotalo

Brilliant!! Now I got it completely!!

Great explanation. Now I knew where I was misunderstanding was that the statement of,

return n + sum(n-1)

sum(n-1) is INDEED the another function (that's why it's a recursive function) to be called and to return it's own local value.

At here,

return n + sum(n-1)

Each function is returning it's own different local value which is also the [b]parameter[/b] of each different functions.

Thanks for clarifying my misunderstanding.

This will make my further study far more interesting and easy.

Cheers!

PS: Hope you will get a good sleep.

: :

: : probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.

: :

: : well, if sum(2) [leftbr]n = 2[rightbr] has been called, it will return [blue]n + sum(1)[/blue]. but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).

: :

: : when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.

: :

: : sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).

: :

: : now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.

: :

: : i'm sleepy, so there might be mistakes in my explanation. forgive me for that ;-)

: : [/purple]

: : [hr][purple]~Donotalo()[/purple]

: :

: :

:

: Hi Donotalo

:

: Brilliant!! Now I got it completely!!

: Great explanation. Now I knew where I was misunderstanding was that the statement of,

:

: return n + sum(n-1)

:

: sum(n-1) is INDEED the another function (that's why it's a recursive function) to be called and to return it's own local value.

:

: At here,

:

: return n + sum(n-1)

:

: Each function is returning it's own different local value which is also the [b]parameter[/b] of each different functions.

:

: Thanks for clarifying my misunderstanding.

: This will make my further study far more interesting and easy.

:

:

: Cheers!

:

: PS: Hope you will get a good sleep.

:

[purple]

but tokoG, one precaution u shud take while programming a recursive function. sometimes it is easy to think recursively and thus it is easy to design a recursive function. but a recursive function may take much more memory than its iterative version. so a recursive function may exhaust memory. though memory shud not be a concern in today's computers, u shud be careful while designing a recursive function in professional life.

[/purple]

[hr][purple]~Donotalo()[/purple]

: : :

: : : probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.

: : :

: : : well, if sum(2) [leftbr]n = 2[rightbr] has been called, it will return [blue]n + sum(1)[/blue]. but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).

: : :

: : : when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.

: : :

: : : sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).

: : :

: : : now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.

: : :

: : : i'm sleepy, so there might be mistakes in my explanation. forgive me for that ;-)

: : : [/purple]

: : : [hr][purple]~Donotalo()[/purple]

: : :

: : :

: :

: : Hi Donotalo

: :

: : Brilliant!! Now I got it completely!!

: : Great explanation. Now I knew where I was misunderstanding was that the statement of,

: :

: : return n + sum(n-1)

: :

: : sum(n-1) is INDEED the another function (that's why it's a recursive function) to be called and to return it's own local value.

: :

: : At here,

: :

: : return n + sum(n-1)

: :

: : Each function is returning it's own different local value which is also the [b]parameter[/b] of each different functions.

: :

: : Thanks for clarifying my misunderstanding.

: : This will make my further study far more interesting and easy.

: :

: :

: : Cheers!

: :

: : PS: Hope you will get a good sleep.

: :

: [purple]

: but tokoG, one precaution u shud take while programming a recursive function. sometimes it is easy to think recursively and thus it is easy to design a recursive function. but a recursive function may take much more memory than its iterative version. so a recursive function may exhaust memory. though memory shud not be a concern in today's computers, u shud be careful while designing a recursive function in professional life.

: [/purple]

: [hr][purple]~Donotalo()[/purple]

:

:

Hi

Yes, my tutorial book says that's why the recursive takes alot of space.

There is another recursive code that I can't quite work out.

It's Fibonacci consequences.

In here, I think the [b]i=0 --return-->0[/b] and [b]i=1 --return-->1[/b] happens right away from the programme execution, no recursive. But when [b]i=2[/b], the recursive occur but how does it happen?

The following is my idea and this calculation is OK until i=3.

But not from i=4. So I think this is wrong.

[b]i=2 .....Output is 1[/b]

return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

[blue](2-1=1)[/blue] + [green](2-2=0)[/green]

[blue]return 1[/blue]+[green]0[/green]

[b]i=3 .....Output is 2[/b]

return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

[blue](3-1=2)[/blue]

[blue](2-1=1)[/blue]

[blue]return 1[/blue]

+

[green]fib(n-2)[/green]

[green](3-2=1)[/green]

[green]return 1[/green]

[code]

#include

int fibonacci(int n) {

if (n == 0)

return 0;

else if (n == 1)

return 1;

else {

return fibonacci(n-1) + fibonacci(n-2);

}

}

void main() {

int i;

printf("The first 10 numbers in the Fibonacci series

");

for (i=0; i<10; i++)

printf("%d ", fibonacci(i));

getchar();

}

[/code]

: : : :

: : : : probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.

: : : :

: : : : well, if sum(2) [leftbr]n = 2[rightbr] has been called, it will return [blue]n + sum(1)[/blue]. but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).

: : : :

: : : : when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.

: : : :

: : : : sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).

: : : :

: : : : now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.

: : : :

: : : : i'm sleepy, so there might be mistakes in my explanation. forgive me for that ;-)

: : : : [/purple]

: : : : [hr][purple]~Donotalo()[/purple]

: : : :

: : : :

: : :

: : : Hi Donotalo

: : :

: : : Brilliant!! Now I got it completely!!

: : : Great explanation. Now I knew where I was misunderstanding was that the statement of,

: : :

: : : return n + sum(n-1)

: : :

: : : sum(n-1) is INDEED the another function (that's why it's a recursive function) to be called and to return it's own local value.

: : :

: : : At here,

: : :

: : : return n + sum(n-1)

: : :

: : : Each function is returning it's own different local value which is also the [b]parameter[/b] of each different functions.

: : :

: : : Thanks for clarifying my misunderstanding.

: : : This will make my further study far more interesting and easy.

: : :

: : :

: : : Cheers!

: : :

: : : PS: Hope you will get a good sleep.

: : :

: : [purple]

: : but tokoG, one precaution u shud take while programming a recursive function. sometimes it is easy to think recursively and thus it is easy to design a recursive function. but a recursive function may take much more memory than its iterative version. so a recursive function may exhaust memory. though memory shud not be a concern in today's computers, u shud be careful while designing a recursive function in professional life.

: : [/purple]

: : [hr][purple]~Donotalo()[/purple]

: :

: :

:

: Hi

:

: Yes, my tutorial book says that's why the recursive takes alot of space.

:

: There is another recursive code that I can't quite work out.

: It's Fibonacci consequences.

:

: In here, I think the [b]i=0 --return-->0[/b] and [b]i=1 --return-->1[/b] happens right away from the programme execution, no recursive. But when [b]i=2[/b], the recursive occur but how does it happen?

:

: The following is my idea and this calculation is OK until i=3.

: But not from i=4. So I think this is wrong.

:

: [b]i=2 .....Output is 1[/b]

: return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

:

: [blue](2-1=1)[/blue] + [green](2-2=0)[/green]

: [blue]return 1[/blue]+[green]0[/green]

:

: [b]i=3 .....Output is 2[/b]

: return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

:

: [blue](3-1=2)[/blue]

: [blue](2-1=1)[/blue]

: [blue]return 1[/blue]

:

: +

:

: [green]fib(n-2)[/green]

: [green](3-2=1)[/green]

: [green]return 1[/green]

:

:

: [code]

: #include

:

: int fibonacci(int n) {

: if (n == 0)

: return 0;

: else if (n == 1)

: return 1;

: else {

: return fibonacci(n-1) + fibonacci(n-2);

: }

: }

:

: void main() {

: int i;

:

: printf("The first 10 numbers in the Fibonacci series

");

: for (i=0; i<10; i++)

: printf("%d ", fibonacci(i));

:

: getchar();

:

: }

: [/code]

:

That's right, the fibonacci sequence is like this (with indexes under):

0 1 1 2 3 5 8 13 ...

0 1 2 3 4 5 6 7

Where's the err?

: :

: : Yes, my tutorial book says that's why the recursive takes alot of space.

: :

: : There is another recursive code that I can't quite work out.

: : It's Fibonacci consequences.

: :

: : In here, I think the [b]i=0 --return-->0[/b] and [b]i=1 --return-->1[/b] happens right away from the programme execution, no recursive. But when [b]i=2[/b], the recursive occur but how does it happen?

: :

: : The following is my idea and this calculation is OK until i=3.

: : But not from i=4. So I think this is wrong.

: :

: : [b]i=2 .....Output is 1[/b]

: : return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

: :

: : [blue](2-1=1)[/blue] + [green](2-2=0)[/green]

: : [blue]return 1[/blue]+[green]0[/green]

: :

: : [b]i=3 .....Output is 2[/b]

: : return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

: :

: : [blue](3-1=2)[/blue]

: : [blue](2-1=1)[/blue]

: : [blue]return 1[/blue]

: :

: : +

: :

: : [green]fib(n-2)[/green]

: : [green](3-2=1)[/green]

: : [green]return 1[/green]

: :

: :

: : [code]

: : #include

: :

: : int fibonacci(int n) {

: : if (n == 0)

: : return 0;

: : else if (n == 1)

: : return 1;

: : else {

: : return fibonacci(n-1) + fibonacci(n-2);

: : }

: : }

: :

: : void main() {

: : int i;

: :

: : printf("The first 10 numbers in the Fibonacci series

");

: : for (i=0; i<10; i++)

: : printf("%d ", fibonacci(i));

: :

: : getchar();

: :

: : }

: : [/code]

: :

: That's right, the fibonacci sequence is like this (with indexes under):

: 0 1 1 2 3 5 8 13 ...

: 0 1 2 3 4 5 6 7

:

: Where's the err?

:

Hi

My quetion is from when [b]i=4[/b].

[b]i=4 .....Output should be 3[/b]

return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

[blue](4-1=3)[/blue]

[blue](3-1=2)[/blue]

[blue](2-1=1)[/blue]

[blue]return 1[/blue]

[red]Is this (Returning 1) the really the case.....?[/red]

+

[green]fib(n-2)[/green]

[green](4-2=2)[/green]

[green](2-2=0)[/green]

[green]return 0[/green]

[red]Is this (Returning 0) the really the case.....?[/red]

I don't understand how (n-1)+(n-2) calculation works in here...

would anyone pls advise?

: : :

: : : Yes, my tutorial book says that's why the recursive takes alot of space.

: : :

: : : There is another recursive code that I can't quite work out.

: : : It's Fibonacci consequences.

: : :

: : : In here, I think the [b]i=0 --return-->0[/b] and [b]i=1 --return-->1[/b] happens right away from the programme execution, no recursive. But when [b]i=2[/b], the recursive occur but how does it happen?

: : :

: : : The following is my idea and this calculation is OK until i=3.

: : : But not from i=4. So I think this is wrong.

: : :

: : : [b]i=2 .....Output is 1[/b]

: : : return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

: : :

: : : [blue](2-1=1)[/blue] + [green](2-2=0)[/green]

: : : [blue]return 1[/blue]+[green]0[/green]

: : :

: : : [b]i=3 .....Output is 2[/b]

: : : return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

: : :

: : : [blue](3-1=2)[/blue]

: : : [blue](2-1=1)[/blue]

: : : [blue]return 1[/blue]

: : :

: : : +

: : :

: : : [green]fib(n-2)[/green]

: : : [green](3-2=1)[/green]

: : : [green]return 1[/green]

: : :

: : :

: : : [code]

: : : #include

: : :

: : : int fibonacci(int n) {

: : : if (n == 0)

: : : return 0;

: : : else if (n == 1)

: : : return 1;

: : : else {

: : : return fibonacci(n-1) + fibonacci(n-2);

: : : }

: : : }

: : :

: : : void main() {

: : : int i;

: : :

: : : printf("The first 10 numbers in the Fibonacci series

");

: : : for (i=0; i<10; i++)

: : : printf("%d ", fibonacci(i));

: : :

: : : getchar();

: : :

: : : }

: : : [/code]

: : :

: : That's right, the fibonacci sequence is like this (with indexes under):

: : 0 1 1 2 3 5 8 13 ...

: : 0 1 2 3 4 5 6 7

: :

: : Where's the err?

: :

:

: Hi

:

: My quetion is from when [b]i=4[/b].

:

: [b]i=4 .....Output should be 3[/b]

: return [blue]fib(n-1)[/blue]+ [green]fib(n-2)[/green]

:

: [blue](4-1=3)[/blue]

: [blue](3-1=2)[/blue]

: [blue](2-1=1)[/blue]

: [blue]return 1[/blue]

: [red]Is this (Returning 1) the really the case.....?[/red]

:

: +

:

: [green]fib(n-2)[/green]

: [green](4-2=2)[/green]

: [green](2-2=0)[/green]

: [green]return 0[/green]

: [red]Is this (Returning 0) the really the case.....?[/red]

:

: I don't understand how (n-1)+(n-2) calculation works in here...

: would anyone pls advise?

:

[purple]think recursively!

u got right when i is in [leftbr]1, 3[rightbr]. so, what will fibonacci(4) return? it will return:

[b]fibonacci(3) + fibonacci(2)[/b]

u've already calculated those values :-) put them and sum them up, u will got the answer.

here both fibonacci(3) and fibonacci(2) are separate functions and take separate portions of the memory.

ok, let me browse through it:[code]

fibo(4)

return [blue]fibo(3)[/blue] + [red]fibo(2)[/red]

[blue]fibo(3)

return [green]fibo(2)[/green] + [purple]fibo(1)[/purple]

[green]fibo(2)

return fibo(1) + fibo(0)

fibo(1)

return 1

fibo(0)

return 0

fibo(2) = 1[/green]

[purple]fibo(1)

return 1

fibo(1) = 1[/purple]

fibo(3) = 2[/blue]

[red]fibo(2)

return fibo(1) + fibo(0)

fibo(1)

return 1

fibo(0)

return 0

fibo(2) = 1[/red]

fibo(4) = 3

[/code]

[/purple]

[hr][purple]~Donotalo()[/purple]