power using recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • greek
    New Member
    • Nov 2006
    • 17

    power using recursion

    the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is
    x= 1, n=0
    1/x^n, n<0
    x*x^(n-1), n>0


    ive written the program it runs but im not having the correct values..here is the code:

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    int power(int n);
    int x,n;
    void main()
    {clrscr();
    cout<<"Input a value for x"<<endl;
    cin>>x;
    cout<<"\n Input a value for n"<<endl;
    cin>>n;
    cout<<"The final answer is"<<power(n);
    getch();
    }
    
    int power(int n)
    { if (n==0)
          return 1;
      else if (n<0)
          return (1/(x*power(n)));
      else
          return (x*(x*power(n-1)));
    }


    i knw the problem comes frm the foll part
    else if (n<0)
    return (1/(x*power(n)));
    else
    return (x*(x*power(n-1)));

    i shud not multiply x by power() but thn hw do i do it??? if i dont multiply i just put it like that

    else if (n<0)
    return (1/(x power(n)));
    else
    return (x*(x power(n-1)));


    i get error!!

    plzzz help
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    should you equation for x^n be
    Code:
          return (x*power(n-1));

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Originally posted by greek
      the algorithm is
      x= 1, n=0
      1/x^n, n<0
      x*x^(n-1), n>0

      Code:
      int power(int n)
      { 
        if (n==0)
            return 1;
        else if (n<0)
            return (1/(x*power(n)));
        else
            return (x*(x*power(n-1)));
      }
      The problem appears to be in your second branch. If n < 0, you will indeed call the recursive function again, but you are passing it the same value of n. This value is negative still, and so you will always execute the second branch. Thus, n never changes, and your end condition is never reached. While you coded your given algorithm correctly, the algorithm itself is wrong. Try this algorithm:

      x^n = 1 / (x ^ |n|), where n < 0 and |n| is the absolute value of n.

      Also, your third branch (for n > 0) multiplies the value by an extra x. It should just be x * power(n-1).

      Next I'd suggest passing power the x value as well, rather than having both values declared as global. This will make the function portable, so you will not have to rewrite a recursive power function ever time you need one.

      Finally, 1 / anything will be a double value, but your function returns an integer. This means that any negative exponent created will be returned as x. You should make this function return a double.

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by greek
        Code:
        int power(int n)
        { if (n==0)
              return 1;
          else if (n<0)
              return (1/(x*power(n)));
          else
              return (x*(x*power(n-1)));
        }
        Ganon11 forgot to mention that your second branch should be returning x*(1/power(n+1)).

        Adrian

        P.S. As Ganon11 stated, You really shouldn't be using global variables Get power to take a param double x. And move the n inside of the main(). Using global variables is a very bad form to get used to and it will bite you in the ass later.

        Comment

        Working...