Thread: Help with a recursive function that computes a to the n

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    11

    Help with a recursive function that computes a to the n

    Hello!

    I have been working on this for several days now and the program has no errors but it does not calculate. Here's the code:
    Code:
    #include "stdafx.h"
    #include "stdio.h"
     
    float power(float a, int n); // function prototype//
     
     
    int main (void)
    {
        float a;
     
        int n;
     
        printf("\n enter a value of a");
    	scanf ("%f", &a);
    	printf("\n enter value of n");
    	scanf ("%f", &n);
        printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n)); 
    	
    	 return 0;
    }
    float  power(float a, int n)
    {
    
        if (a==0) 
        {
            return 0;
     
        }
        else if(n==0)
        {
            return 1;
     
        }
        else if (n>0)
        {
            return( a* power(a,n-1));
        }
        else
        {
            return ((1/a)*power(a,n+1));
        }
    }
    Please help me figure this out!
    Last edited by Salem; 10-02-2008 at 11:38 AM. Reason: Added [code][/code] tags, learn how to use them yourself!!!!!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What's seems to be the problem? What is the output?

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >> return ((1/a)*power(a,n+1));

    Really?

    The other problem with doing it this way is that your order of operations is out of whack too.
    Last edited by master5001; 10-02-2008 at 11:48 AM.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by master5001 View Post
    >> return ((1/a)*power(a,n+1));

    Really?

    The other problem with doing it this way is that your order of operations is out of whack too.
    No that looks right. a^-3 = (1/a)*a^-2 = (1/a)*(1/a)*a^-1=(1/a)*(1/a)*(1/a)*1.

    He could compute 1/(a^n) in that final condition, but this should work almost as well.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah ok, fair enough, it is going "less than 0." Though I am still going to point out that one could theoretically be compounding floating point errors. I would still only do my division once to minimize floating point errors. But I am also one of those crazy types of guys who would typically subtract 1.0f from a floating value too....

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yes, that's why I said almost as well. But if the concern is precision, then the first thing to fix is to change float to double.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Either way, I am not spotting anything that would cause any sort of unusual returns. I would use a double for this. And even with a double I would still do: return 1.0 / power(a, -n); But that is just me.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    When you're inputting n, being an integer, you should use

    scanf ("%d", &n);
    instead of
    scanf ("%f", &n);

    [sound of "doh!!"]

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    11

    The issue is when you run the program.

    Quote Originally Posted by C_ntua View Post
    What's seems to be the problem? What is the output?
    The problem is that the program does not seem to have the correct output. I attached a screenshot to explain what I mean!

    Thanks!

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by nonoob View Post
    When you're inputting n, being an integer, you should use

    scanf ("%d", &n);
    instead of
    scanf ("%f", &n);

    [sound of "doh!!"]

    Uh oh... Todd has some competition for the title of Ol' Eagle Eye. That looks like reason enough for your code to be broken by my count.

    Being in a good mood today, here is a recap of all issues we have addressed.

    Suggestions implemented:
    Code:
    #include "stdafx.h"
    #include "stdio.h"
     
    float power(float a, int n); // function prototype//
     
     
    int main (void)
    {
        float a; 
        int n;
     
        printf("\n enter a value of a");
        scanf ("%f", &a);
        printf("\n enter value of n");
        scanf ("%d", &n);
        printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n)); 
    
        return 0;
    }
    float  power(float a, int n)
    {
        /* I put the numbers in float notation. Not a big thing. I am nit-picking. */
        if (a==0.0f) 
        {
            return 0.0f;
     
        }
        else if(n==0)
        {
            return 1.0f;
         }
        else if (n>0)
        {
            return( a* power(a,n-1));
        }
        else
        {
            return (1.0f / power(a,-n)); // 1 extra recursion. 1 division
        }
    }

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    Thank you all for your help! Now, what can I do to turn this into a non-recursive function.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Write it in a non-recursive way. If only there was a way to repeat a certain group of code while some condition remains true....

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Ooooh ooooh ooooh pick me pick me!!!

    Gotos?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nonoob View Post
    Ooooh ooooh ooooh pick me pick me!!!

    Gotos?
    Now, now, Johnny, you know what happens when you say ... that word.

    /me throws tennis ball at head.

    Anyone else?

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    Here is the code I am trying to compute for a non- recursive function now!
    #include "stdafx.h"
    #include "stdio.h"

    double mypower(float a, int n); // function prototype//


    int main (void)
    {
    float a;
    int n;
    int power;

    printf("\n enter a value of a\n");
    scanf ("%d", &a);
    printf("\n enter value of n\n");
    scanf ("%d", &n);
    power=mypower (a,n);
    printf("The result is %d\n", power);

    return 0;
    }
    double power(float a, int n)
    {
    if (a==0.0)
    {
    return 0.0;

    }
    else if(n==0)
    {
    return 1.0;
    }
    else if (n>0)
    {
    return( a* mypower(a,n-1));
    }
    else
    {
    return (1.0/ mypower(a,-n));
    }
    }


    The error is error LNK2019: unresolved external symbol "double __cdecl mypower(float,int)" (?mypower@@YANMH@Z) referenced in function _main
    C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\lab 8.3.2\Debug\lab 8.3.2.exe : fatal error LNK1120: 1 unresolved externals

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM