Thread: help

  1. #1
    cman
    Guest

    help

    hi all, anyone see anything wrong with this? it prints out the days but the population or rather the fib calculation does not function.

    Code:
    #include <stdio.h>
    #define MAX_LIMIT 10000000
    
    long fib( long );
    
    int main()
    {
    	int day;
    
    	for( day = 1; (fib(day) < MAX_LIMIT); day++ )
    		printf( "day:%d\t population:%ld\n", day, fib(day));
    
    	return 0;
    }
    
    long fib( long num )
    {
    	int n;
    	
    	if (n == 0) 
    		return 0;
    	else if ( n == 1 )
    		return 1;
    	else 
    		return fib(n-1) + fib(n-2);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Didn't your compiler warn you:
    Code:
    Parameter 'num' is never used in function fib
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    cman
    Guest
    no it didnt warn me but after changing it
    i still get the same problem
    it loops and outputs the days and the population remains 0 always

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    Re: help

    I've got no idea what this thing does, or what the output is supposed to look like but scrapping int n and using num in the fib function gives a final output of

    day:35 population:9227465
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    cman
    Guest
    Code:
    long fib( long num )
    {
    	if (num == 0) 
    		return 0;
    	else if ( num == 1 )
    		return 1;
    	else 
    		return fib(num-1) + fib(num-2);
    }
    i changed it and it worked thanks ronin. But the weird thing is that dont i have to define num inside the brackets for what type it is? or is that already done in the function prototype?

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    C Board > General Programming Boards > C Programming > help
    I hope you know that you just ruined 50% of the replies you'd get if you were actually more specific with your subject title.

    Obviously you're here for help.

    > long fib( long num )
    > is that already done in the function prototype?
    It's done right there, the function title/lable, whatever. Try using a variable that isn't in the function title like, FooChar.

    Use it in this way

    FuncTitle ( stuff )
    {
    /* stuff */
    if ( FooChar )
    etc
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed