Recursion

This is a discussion on Recursion within the C Programming forums, part of the General Programming Boards category; I am having difficulty with an assignment that has to deal with recursion in C. We were asked to create ...

  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,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,185
    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,185
    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,185
    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, 08: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, 09: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, 08: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, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21