-
February 16th, 2014, 03:56 PM
#1
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
Last edited by Jose M; February 16th, 2014 at 06:01 PM.
-
February 16th, 2014, 05:03 PM
#2
Re: fibonacci number
What are the variables post1 and post2 - as opposed to pos1 and pos2?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 16th, 2014, 06:02 PM
#3
Re: fibonacci number
Sorry ... post1 and post2 is a mistake.
I meant pos1 and pos2, are the same.
-
February 16th, 2014, 05:26 PM
#4
Re: fibonacci number
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.
Last edited by razzle; February 16th, 2014 at 05:31 PM.
-
February 16th, 2014, 05:33 PM
#5
Re: fibonacci number
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().
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 16th, 2014, 05:45 PM
#6
Re: fibonacci number
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, 06:19 PM
#7
Re: fibonacci number
Razzle:
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 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, 07:30 PM
#8
Re: fibonacci number
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.
Last edited by razzle; February 16th, 2014 at 08:14 PM.
-
February 17th, 2014, 04:49 AM
#9
Re: fibonacci number
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.
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 16th, 2014, 06:37 PM
#10
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?
Last edited by 2kaud; February 16th, 2014 at 06:45 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 17th, 2014, 02:36 AM
#11
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.
compiler g++. OS: ubuntu linux
The compiler and SO are right. I am who is not right.
Thanks
-
February 17th, 2014, 08:06 AM
#12
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).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|