Thread: Problem with recursive function for an expression

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Problem with recursive function for an expression

    Hey everyone i need help with this function that displays the result of the following expression : expr=1/1+2/2+3/3+5/4+8/5+...
    where the numerator represents a fibonaci number.
    Code:
    #include<stdio.h>
    int fibo(int n)
    {
        if(n==0 || n==1)
        return 1;
        return fibo(n-1)+fibo(n-2);
    }
    
    
    float expr(int n)
    {
        if(n==1)
        return 1;
        return (fibo(n)/(n))+expr(n-1);
    
    
    }
    
    
    int main()
    {
        int n,m=1;
        printf("dami n");
        scanf("%d",&n);
    
    
        printf("fibonaci de %d=%d\n",n,fibo(n));
        printf("expresie de %d=%f",n,expr(n));
    }

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Compile with warnings enabled, and your compiler will help you:
    Code:
    sanzor.c: In function ‘main’:
    sanzor.c:22:11: warning: unused variable ‘m’ [-Wunused-variable]
    sanzor.c:24:10: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
    sanzor.c:29:1: warning: control reaches end of non-void function [-Wreturn-type]
    Are you aware that float type has just seven significant digits or precision? You might wish to use double for this kind of thing.

    Edit 1: Fibonacci(0) = 0, Fibonacci(1) = 1, Fibonacci(2) = 1. Your fibo() function has wrong seed values (Fibonacci(1) = 1, Fibonacci(2) = 1).

    Edit 2: If you have two integer variables or expressions, / does an integer division. Therefore, on line 14, your code does an integer division, discarding any fractional part of the result. To calculate the division using floating-point values, cast one or both to float or double.

    In other words, if you have integer variables or expressions a and b, then a/b is also an integer: the fractional decimals have been discarded.

    If you want the result as a double, then use a/(double)b for example. That casts b to double type, thus promoting the result to double type too.
    Last edited by Nominal Animal; 11-28-2012 at 11:25 AM.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Thank you i thought that if the function returns a float result it is enough...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a recursive expression tree program
    By TeamRival in forum C Programming
    Replies: 6
    Last Post: 03-25-2011, 07:27 PM
  2. Problem with recursive function
    By Posto2012 in forum C Programming
    Replies: 5
    Last Post: 12-05-2009, 03:32 AM
  3. Recursive function problem
    By trnd in forum C Programming
    Replies: 5
    Last Post: 01-30-2009, 12:36 AM
  4. recursive function problem
    By jk81 in forum C Programming
    Replies: 2
    Last Post: 10-25-2002, 06:02 AM
  5. Problem with a recursive function
    By fofone in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 08:21 AM