By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

power using recursion

P: 17
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:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. int power(int n);
  5. int x,n;
  6. void main()
  7. {clrscr();
  8. cout<<"Input a value for x"<<endl;
  9. cin>>x;
  10. cout<<"\n Input a value for n"<<endl;
  11. cin>>n;
  12. cout<<"The final answer is"<<power(n);
  13. getch();
  14. }
  15.  
  16. int power(int n)
  17. { if (n==0)
  18.       return 1;
  19.   else if (n<0)
  20.       return (1/(x*power(n)));
  21.   else
  22.       return (x*(x*power(n-1)));
  23. }


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
Feb 17 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
should you equation for x^n be
Expand|Select|Wrap|Line Numbers
  1.       return (x*power(n-1));
  2.  
Feb 17 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
the algorithm is
x= 1, n=0
1/x^n, n<0
x*x^(n-1), n>0

Expand|Select|Wrap|Line Numbers
  1. int power(int n)
  2.   if (n==0)
  3.       return 1;
  4.   else if (n<0)
  5.       return (1/(x*power(n)));
  6.   else
  7.       return (x*(x*power(n-1)));
  8. }
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.
Feb 17 '07 #3

AdrianH
Expert 100+
P: 1,251
Expand|Select|Wrap|Line Numbers
  1. int power(int n)
  2. { if (n==0)
  3.       return 1;
  4.   else if (n<0)
  5.       return (1/(x*power(n)));
  6.   else
  7.       return (x*(x*power(n-1)));
  8. }
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.
Feb 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.