Thread: Recursion

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Recursion

    Code:
    #include<stdio.h>
    
    int Fibonacci(int 2)
    
    {
    	if(1==0)
    
    		return 0;
    
    	else if(1==1)
    
    		return 1;
    
    	else
    
    		return (Fibonacci(2-1)+Fibonacci(2-2));
    }
    
    void main()
    {
    	int end,i;
    
    	printf("Enter>");
    	scanf("%d",&end);
    
    	for(i=1;i<end;i++)
    
    		printf("%d",Fibonacci(i));
    
    	printf("\n");
    }
    
    I have three errors:
    
    C:\Users\jian\Desktop\Cpp1.cpp(4) : error C2143: syntax error : missing ')' before 'constant'
    C:\Users\jian\Desktop\Cpp1.cpp(4) : error C2143: syntax error : missing ';' before 'constant'
    C:\Users\jian\Desktop\Cpp1.cpp(4) : fatal error C1004: unexpected end of file found
    
    But I do not know what they mean. Pls help.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    I doubt that the file you posted is the same file you are compiling, or perhaps you have some strange character that the compiler does not recognize (perhaps if you are using a rich text editor)
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jian View Post
    C:\Users\jian\Desktop\Cpp1.cpp(4) : error C2143: syntax error : missing ')' before 'constant'
    C:\Users\jian\Desktop\Cpp1.cpp(4) : error C2143: syntax error : missing ';' before 'constant'
    C:\Users\jian\Desktop\Cpp1.cpp(4) : fatal error C1004: unexpected end of file found

    But I do not know what they mean. Pls help.
    When fixing compiler errors, always start with the first one, as fixing that often fixes subsequent errors. Then, read up on the rules for C identifiers. Look at the parameter list for your Fibonacci function. Notice anything wrong with it?

    Also, your if statements are bogus. They never take into account the parameter you pass in. The first one is always false (one is never equal to zero), so it will be skipped. The second one is always true, so your function will always return one, and the last one also never gets executed.

    A couple other things to fix:
    It's int main(void) and return 0 when you're done
    Put some spaces or a newline between the numbers you print out so we can tell what's going on.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Duh. Missed that one.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    You can't define a variable name by a number. 2 is not a valid variable name.

    And even if it was, what would possess you to do it?!
    C is fun

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    I got it now, all I have to do is change the numbers to 'n'. Somehow I got confused along the way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM