# fibonacci number

• February 16th, 2014, 02:56 PM
Jose M
fibonacci number
Hi, the next code is right. I mean it works. It gives the sequence of fibonaci numbers ...

Code:

```#include <iostream> #include <iomanip> using namespace std; int fibo(int pos) {     if(!pos)return 0;   if(pos==1)return 1;   int pos2=--pos;   int pos1=--pos;   int r1=fibo(pos1);   int r2=fibo(pos2);   int r=r1+r2;   return r; } int main() {   int number;   cin>>number;     for(int p=0;p<number;++p)     {       int r=fibo(p);       cout<<p<<"  "<<r<<endl;     }   cout<<endl;   return  0; }```
but....
when I chage the lines:
Code:

```int r1=fibo(pos1);   int r2=fibo(pos2);   int r=r1+r2;```
for the lines:

Code:

`int r=fibo(pos1)+fibo(pos2);`
It doen't work right. Apararently it should work the same.
Can anyone give an explanation to this...?????

Thanks
• February 16th, 2014, 04:03 PM
2kaud
Re: fibonacci number
What are the variables post1 and post2 - as opposed to pos1 and pos2?
• February 16th, 2014, 04:26 PM
razzle
Re: fibonacci number
Quote:

Originally Posted by Jose M
Apararently it should work the same.

Maybe you didn't copy right? Why don't you ask the one who wrote the code! :)

From a principal standpoint, the two approaches you're trying here are iteration and recursion and they will look very different. You simply have to understand what you're doing to get it right. Insight first.
• February 16th, 2014, 04:33 PM
2kaud
Re: fibonacci number
Quote:

Originally Posted by razzle
From a principal standpoint, the two approaches you're trying here are iteration and recursion and they will look very different.

They are both recursion on the function fibo().
• February 16th, 2014, 04:45 PM
razzle
Re: fibonacci number
Quote:

Originally Posted by 2kaud
They are both recursion on the function fibo().

Then that's the problem because iteration and recursion don't work the same.
• February 16th, 2014, 05:02 PM
Jose M
Re: fibonacci number
Sorry ... post1 and post2 is a mistake.
I meant pos1 and pos2, are the same.
• February 16th, 2014, 05:19 PM
Jose M
Re: fibonacci number
Razzle:

Quote:

From a principal standpoint, the two approaches you're trying here are iteration and recursion and they will look very different.
Do you mean that this is iteration... ?
Code:

```int r1=fibo(pos1);   int r2=fibo(pos2);   int r=r1+r2;```
And this is recursion...?
Code:

`int r=fibo(pos1)+fibo(pos2);`
I agree with 2kaud:
Quote:

Quote Originally Posted by 2kaud View Post
They are both recursion on the function fibo().
I need a clearer answer.... come on!

And for you is clear that that is the reason they have to work different ????
• February 16th, 2014, 05:37 PM
2kaud
Re: fibonacci number
Well on my MSVS system, both versions of the code produce the same output!

For an input of 10, the output is
Code:

```0  0 1  1 2  1 3  2 4  3 5  5 6  8 7  13 8  21 9  34```
Even slightly simplying your code still produces the same output
Code:

```int fibo(int pos) {     if (pos < 2) return pos; int pos2 = --pos; int pos1 = --pos;     return fibo(pos1) + fibo(pos2); }```
or even this on my system produces the same output
Code:

```int fibo(int pos) {         if (pos < 2) return pos;         return fibo(pos - 2) + fibo(pos - 1); }```
What os/compiler are you using?
• February 16th, 2014, 06:30 PM
razzle
Re: fibonacci number
Quote:

Originally Posted by Jose M
I need a clearer answer.... come on!

There's iteration and there's recursion.

They said so in class and I told you here.

You listen up now.
• February 17th, 2014, 01:36 AM
Jose M
Re: fibonacci number
ok...
I am sorry...
I have checked it again. I have compiled them again and now yes I get the same result.
I dont understand what happened. I have to think it was something else.:confused:

compiler g++. OS: ubuntu linux

The compiler and SO are right. I am who is not right.:)

Thanks
• February 17th, 2014, 03:49 AM
2kaud
Re: fibonacci number
Quote:

Originally Posted by razzle
There's iteration and there's recursion.

Yes, there is both iteration and recursion and what Jose M is using here is recursion NOT iteration. Fibonacci can be coded using both but his code is recursion.

Quote:

They said so in class and I told you here.

You listen up now.
Perhaps you didn't listen properly in your class. :( When posting, please remember that politeness costs nothing.
• February 17th, 2014, 07:06 AM
OReubens
Re: fibonacci number
recursion:
When a function makes use of calls to itself to get it's job done.

I have some slight objections to calling the other way "iteration", more correctly would be "non-recursive" or "without recursion".

THe issue here being that you can solve fib(n) in several different non-recursive ways which may not even be iterative, and you can even use recursion without the double recursion as used here (which makes it terribly slow for larger values).