CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2014
    Posts
    11

    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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,821

    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)

  3. #3
    Join Date
    Jul 2013
    Posts
    576

    Re: fibonacci number

    Quote Originally Posted by Jose M View Post
    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.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,821

    Re: fibonacci number

    Quote Originally Posted by razzle View Post
    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)

  5. #5
    Join Date
    Jul 2013
    Posts
    576

    Re: fibonacci number

    Quote Originally Posted by 2kaud View Post
    They are both recursion on the function fibo().
    Then that's the problem because iteration and recursion don't work the same.

  6. #6
    Join Date
    Feb 2014
    Posts
    11

    Re: fibonacci number

    Sorry ... post1 and post2 is a mistake.
    I meant pos1 and pos2, are the same.

  7. #7
    Join Date
    Feb 2014
    Posts
    11

    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 ????

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,821

    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)

  9. #9
    Join Date
    Jul 2013
    Posts
    576

    Re: fibonacci number

    Quote Originally Posted by Jose M View Post
    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.

  10. #10
    Join Date
    Feb 2014
    Posts
    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

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,821

    Re: fibonacci number

    Quote Originally Posted by razzle View Post
    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)

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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
  •  





Click Here to Expand Forum to Full Width

Featured