Thread: Recursion

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    18

    Recursion

    I am having difficulty with an assignment that has to deal with recursion in C.

    We were asked to create a program (using recursion) to sum up a harmonic series.

    This is what I have so far:
    Code:
    #include <stdio.h>
    
    long Harmonic ( int n );  
    int main()
    {
        int seriesSize;
        printf("This program prints a harmonic series.\n");
        printf("How many numbers do you want?\n");
        
        scanf("%d", &seriesSize); 
        
        printf ("The sum of this harmonic series is: " "%ld\n", Harmonic(seriesSize));
        
        system("pause");
    }
    
    long Harmonic ( int n )
    
    {
        long result;
        if ( n == 0 )                    
            result = 1;
        else
            result =(Harmonic(n-1)+(1/n));
        return result; 
        
    }
    I am having trouble with the output, it doesn't return the correct value.

    Is my harmonic equation incorrect? Where am I going wrong?

    Thanks for your help in advance!

    ~AB

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1/n = 0 in all the cases where you do that division (well, 0 remainder 1, but you didn't ask for the remainder). If perhaps you want a decimal, you need to do 1.0/n. Also, you don't want an integral return type (long is an integer), but one of the floating-point types (float or double).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1/n is going to be 0, for any n except 1.
    Perhaps use doubles rather than ints?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    yeah, i should've picked up on that...lol

    I usually always use double for my assignments, just in case.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    There is still something wrong with my equation...I have tried several iterations with the same result. Any hints?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You did remember to change your printf specifier to &#37;f when you changed the return type to double, right?

    Edit: Also your final answer is probably 1.0 too large, since your base case is incorrect.
    Last edited by tabstop; 10-26-2008 at 01:28 PM.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    I've tried both of these base cases

    if n==0
    return 0

    if n==1
    return 1

    and neither come up with a result even close to what it is supposed to be.
    I have also tried other recursive steps:

    (1/(Harmonic(n-1)))

    1 + (1/(Harmonic(n-1)))

    I have no idea why this is not working.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    #include <stdio.h>
    
    double Harmonic ( int n );  
    int main()
    {
        int seriesSize;
        printf("This program prints a harmonic series.\n");
        printf("How many numbers do you want?\n");
        
        scanf("%d", &seriesSize); 
        
        printf ("The sum of this harmonic series is: " "%f\n", Harmonic(seriesSize));
        
        system("pause");
    }
    
    double Harmonic ( int n )
    
    {
        double result;
        if ( n == 1 )                    
            result = 1;
        else
            result =(Harmonic(n-1)+(1.0/n));
        return result; 
        
    }

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    works perfectly, thanks

    wow - that one decimal place makes all the difference?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's what we said at the beginning -- 1/n is always zero, except when n is 1.

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... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  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