Fibonacci Sequence - Function within C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nikid72
    New Member
    • Mar 2010
    • 2

    #1

    Fibonacci Sequence - Function within C

    I am having a problem with a program I have written, the requirements were to

    "Write a function that computes and returns the n-th Fibonacci number. The main should invoke that function and print out the 10, 20, and 30th Fibonacci numbers. Consider that the first two numbers are 0 and 1."

    I'm not sure what I'm doing wrong, and any suggestions would be appreciated!!!

    Code:
    #include <stdio.h>
    
       int funcfib(int n1);
     
       int main( )
       {
    	   int n;
    
    	   funcfib ( n );
       }
    
      int funcfib (int n1)
       {
    	   int answer;
    
    	   printf ( "Enter number to find, enter 0 to exit: ");
    
    	   scanf ("%d", &n1);
    
    	   if (n1 > 0){
    
    			if ( n1 < 3 )
    				return (1);
    			else 
    				return( funcfib(n1-2) + funcfib(n1-1));
    	   }
    
    	   answer = funcfib (n1);
    
    	   printf ("\n\n");
    
    	   printf (" %dth Fibonacci Number is %d\n", n1, answer);
      
       }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Line 16 - 18 Requesting user input in a recursively called function seems odd. Do you really want the user to re-input the "number to find" on every iteration?

    Line 22 - 23 The first 2 numbers are 0 and 1 but you return 1 for both of them

    Line 28 You get here if n1 < 0 and recursively call the function with the same number. Is that really what you want?

    Line 32 You print the answer from a recursive function. Normally the answer from a recursion happens at the end of all recursions not once for every recursion.


    From your code it looks like you have tried to mix a reusive function to do the calculation and a non recursive function to control calling the recursive function into 1 function which is probably a bad idea.

    Comment

    • nikid72
      New Member
      • Mar 2010
      • 2

      #3
      I'm not sure if I understand completely....I need to be able to have the user input the number to be evaluated. Should I have lines 16-18 in the main function?

      Should I create 2 functions, 1 to gather user input and the other to do the calculations? I have really confused myself with this one....

      Here is my first try with this program, it works but it only takes 1 user input and then closes. Thanks for the help!

      Code:
      #include <stdio.h>
      
         int funcfib(int n);
       
         int main( )
         {
          int n, answer;
      
          printf ( "Enter number to find, enter 0 to exit: ");
      
          scanf ("%d", &n);
      
          answer = funcfib (n);
      
      	printf ("\n\n");
      
      	printf (" %dth Fibonacci Number is %d\n", n, answer);
      
      	
      	     
        return 0;
         }
      
        int funcfib (int n)
         {
           if ( n < 3 )
           {
               return (1);
           }
      	 else 
           {
             return( funcfib(n-2) + funcfib(n-1));
           }
         }
      Last edited by nikid72; Apr 5 '10, 12:08 AM. Reason: added information

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Either getting user input in the main function or having a function that gets user input and returns it would work. However read your own problem description, there is no requirement for user input.

        If you want multiple inputs and multiple results you will need a loop and some way of telling when to end the loop.

        P.S. Please put [code[i]]...[/code] tags round your code.

        Comment

        Working...