fibonacci series using recursion.. error not an allowed type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • greek
    New Member
    • Nov 2006
    • 17

    fibonacci series using recursion.. error not an allowed type

    Hi!
    I hav to generate fibonaaci series using recursion:
    0,1,1,2,3,5,8,1 8,21...
    whr
    fibonacci(0)=0
    fibonacci(1)=1
    fibonacci(n)=fi bonacci(n-1)+fibonacci(n-2)

    ive witten the code but having 2 errors:

    #include<iostre am.h>
    #include<conio. h>
    void fibonacci(int i);
    int n,x,f;
    void main()
    {clrscr();
    for(n=0;n<100;n ++)
    fibonacci(x);
    getch();
    }

    void fibonacci(int i)
    {if(i==0)
    f=0;
    else if(i==1)
    f=1;
    else
    f=fibonacci(i-1)+fibonacci(i-2); // NOT AN ALLOWED TYPE
    }


    the error is not an allowed type..wats that???
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    as you are using the function fibonacci() in the expression
    Code:
    f=fibonacci(i-1)+fibonacci(i-2); // NOT AN ALLOWED TYPE
    it should return an int as the function result not void, e.g.
    Code:
    int fibonacci(int i)
    {if(i==0)
    f=0;
    else if(i==1)
    f=1;
    else
    f=fibonacci(i-1)+fibonacci(i-2); // NOT AN ALLOWED TYPE
    }

    Comment

    • greek
      New Member
      • Nov 2006
      • 17

      #3
      Originally posted by horace1
      as you are using the function fibonacci() in the expression
      Code:
      f=fibonacci(i-1)+fibonacci(i-2); // NOT AN ALLOWED TYPE
      it should return an int as the function result not void, e.g.
      Code:
      int fibonacci(int i)
      {if(i==0)
      f=0;
      else if(i==1)
      f=1;
      else
      f=fibonacci(i-1)+fibonacci(i-2); // NOT AN ALLOWED TYPE
      }
      Ok thx but now the problem is that when im cout the fibonacci(x)
      i get only a list of zeros..the fibonacci series is not being generated
      wat do i do?

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by greek
        Ok thx but now the problem is that when im cout the fibonacci(x)
        i get only a list of zeros..the fibonacci series is not being generated
        wat do i do?
        in main() your loop counter is n but you call fibonacci with paramater x, it should be n, e.g.
        Code:
        for(n=0;n<10;n++)
        cout << fibonacci(n)<< endl;
        you also need to return f at the end of fibonacci

        Comment

        Working...