Thread: Harmonic Sums Recursive Code

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    36

    Harmonic Sums Recursive Code

    Can someone please tell me what I am doing wrong? I cannot figure it out. Maybe it is my math somewhere. In short, the nth harmonic number is defined as 1 + ½ + ⅓ + … + 1/n. The base case is Harmonic Sum 1 equals one.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        double n, soln;
        double harmonic_num (double num);
        
        printf("Harmonic numbers \n");
        
        do
        {
            
            printf("Enter a positive number (or 0 to quit) \n");
            scanf("%lf", &n);
            
            if (n == 0)
                {
                    printf("Goodbye! \n");
                    system("PAUSE");
                    return;
                }
            
            if (n == 1)
            {
                printf("Harmonic number %lf is %.6lf", n, harmonic_num(n));
                return;
            }
            else
            
            soln = harmonic_num(n);
            
            
            printf("Harmonic number %.0lf is %.6lf", n, soln);
            printf("\n");
            
        } while (n != 0);
        
        
        system ("PAUSE");
        return 0;   
    }
    
    //Harmonic Function
        double harmonic_num (double num)
        {
            double k;
            double value = (1/num);
            k = num;
            
            while (k <= 1)
            {
                value += 1+ AZ`(1/(k-1));
                k--;
            }
            return value;
        }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        double harmonic_num (double num)
        {
            double k;
            double value = (1/num);
            k = num;
            
            while (k <= 1)
            {
                value += 1+ AZ`(1/(k-1));
                k--;
            }
            return value;
        }
    I'm not really sure what you're doing here. From your description, it seems like you should just be doing:
    Code:
    double harmonic( int n )
    {
        double s = 1.0;
        do
        {
            s += 1.0 / n;
        }
        while( --n > 1 );
    
        return s;
    }
    Or am I just not understanding what it is you're trying to do?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    id have a closer look at that harmonic num function

    it doesnt do much

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    36
    Got it. I was making a very simple mistake. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Recursive Function in Pseudo Code
    By smitsky in forum Tech Board
    Replies: 3
    Last Post: 10-24-2004, 10:17 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM