Calculating Fibonacci Numbers Recursively in C

后端 未结 6 1172
名媛妹妹
名媛妹妹 2021-01-14 01:52

I\'m trying to learn C by writing a simple program to output Fibonacci numbers. It isn\'t working.

fibonacci.h

unsigned int fibonacc         


        
相关标签:
6条回答
  • 2021-01-14 02:32

    You need \n not %n for your printf. Also, you can simplify as:

    #include "fibonacci.h"
    
    unsigned int fibonacci_recursive(unsigned int n) {
    if (n < 2) 
        return n;
    else
        return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
    }
    
    0 讨论(0)
  • 2021-01-14 02:33

    You haven't created a fibonacci_recursive function that you declared in fibonacci.h.

    0 讨论(0)
  • 2021-01-14 02:37

    You have the main() function defined twice in your project. This is the entry point of your program, and you only need one.

    0 讨论(0)
  • 2021-01-14 02:47

    You declared two main() functions, and the new line character is '\n'.

    0 讨论(0)
  • 2021-01-14 02:49

    Your approach seems strange, you should have:

    • a main file (example main.c) with the main method and that includes fibonacci.h
    • a fibonacci.h with the prototype unsigned int fibonacci_recursive(unsigned int n);
    • a fibonacci.c with the implementation of the method, and it should include fibonacci.h too

    Actually you define main function twice too..

    main.c

    #include <stdio.h>
    #include "fibonacci.h"
    
    main()
    {
        unsigned int i;
        for (i = 0; i < 10; i++)
        {
            printf("%d\t%n", fibonacci_recursive(i));
        }
        getchar();
    }
    

    fibonacci.h

    unsigned int fibonacci_recursive(unsigned int n);
    

    fibonacci.c

    #include "fibonacci.h"
    unsigned int fibonacci_recursive(unsigned int n)
    {
        if (n == 0) 
        {
            return 0;
         } 
         if (n == 1) {
               return 1;
         }
         return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
    }
    
    0 讨论(0)
  • 2021-01-14 02:50

    Well, I preface that recursive function is not an efficient method to calculate Fibonacci and it may be used for dev training/demonstrations purposes only, because every recursion is stored in stack, and it may also overflow for large fibonacci numbers. It is rather worth the effort to write down a more efficient Fibonacci function that uses a loop, like following code:

    #include <stdio.h>
    
    #define MAX_ITERS 20
    
    
    int fibonacci(int);
    
    int main(int argc, char *argv[])
    {
        unsigned int iters;
    
        if(argc>1) {
            iters=atoi(argv[1]);
        } else
            iters=MAX_ITERS;
    
        fibonacci(iters);
    
        return 0;
    }
    
    int fibonacci(int iterations)
    {
       unsigned register int i;
       double first=0.0, second = 1.0, lastsum;
       printf("First %d iterations of Fibonacci series are :\n",iterations);
    
       for ( i = 0 ; i < iterations ; i++ )
       {
          if ( i <= 1 )
             lastsum = (double)i;
          else
          {
             lastsum = first + second;
             first = second;
             second = lastsum;
          }
          printf("%.0f\n",lastsum);
       }
    
    }
    

    Try to compare by your own, running ./fibonacci 50 with this method, for instance on a low cost processor (eg. on a Raspberry PI), and the one with the recursive functions and 50 first numbers as well, and see the difference! ,-)

    0 讨论(0)
提交回复
热议问题