negative power function

Hi people!!!

Appealing again for your help.

I have an assignment to write a RECURSIVE power function which will be able to rise base into positive as well as to negative exponents.

My problem is that when I input negative exponent program always returns 1, like if the exponent is 0.

Does anybody have any idea what to do?
manY thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

double rasing(double x, double y);

int main(){
  double base, exponent;
  
  cout<< "Please input the base and exponent: " ;
  cin>> base >> expoenent;

 cout<< rasing(base,exponent);  
 cout<< endl;

return 0;
}

double rasing(double x, double y){
  
   if(y == 0){
     return 1;
   }
   
   else if(y == 1){
     return x;
   }

   else if(y > 0){
   return x * (rasing(x, y-1));
   double result = x;
  }
  
  else if (y < 0){
    return 1/( x * (rasing(x, y-1)));
    double result = x;
  }
  
 }
Last edited on
Well in line 30...I am a little confused on why you are actually calling the function that you are already in again..Enlighten me please, as im in my first semester of my ass. in CS.

and also to find a negative power, dont you turn the number into a fraction.. so if it was 20-2 it would be 1/202

help me if im wrong lol, im at ur level too
hi, line 30 is actually to output the power result. if I remove it the return value will be always 1. the same problem which I have with negative.

I also tried to devide x * (rasing(x, y-1)); on 1, but it gives incorrect output and in most cases returns 1. :(
im writing this whole program over using loops..because I dont even understand your logic on the functions haha we are just finishing that chapter
and my way worked..I dont know why i cannot grasp ur concept. I tested yours out and it works for the normal raising to the powers but the negative ones dont work...doing it with for loops i just changed the exponent to a positive by multiplying it by -1 and then changing the base by dividing it on 1.
The program is correct for the most part. The only major correction I see that needs to be made is that on line 34, the second parameter to the raising() call should be -y-1, not y-1. Otherwise you'll cause infinite recursion.
Also, the parameters for raising() should be int, not double. The code doesn't behave well with fractional values.
There are other minor problems, but the program should behave correctly even with them.

hi, line 30 is actually to output the power result. if I remove it the return value will be always 1.
Nope. Lines 30 and 35 never execute because return gives the control flow back to the caller. Those lines do nothing at all; the compiler may even be ignoring them.
Last edited on
Here is your code.
1
2
3
4
5
6
else if (y < 0)
  {
    return (1/x)*(rasing(x,y+1));
	
   
  }
helios is perfectly correct.
I really dont understand how this is raising to a power, it doesnt use the funtion pow and it doesnt use loops..so how does this raise to the power?

return x * (rasing(x, y-1));

Does it have something to do with the y-1 part?, I dont understand why you need to put two permiters in there again... Can anybody explain the logic to me.
Last edited on
Helios, but if I remove line 35 and 30 the program will always output 1. Because it will eventually reduce Y zero and if statement will be executed.

But If I put those lines it gives me the correct output. Do you know why?


Ledien, Thankyou very very much!!!!!!!!!!!!!!!
CuLLen look, this program is an example of a recursion. (You may google it) You would probably handle it the next sem. The main point of it is that the function calls itself but reduces Y on one, until it reaches 0.

like for example base is 4 exp is 3 it multiplies 4 on 3 three times: 4 *3 = 12 than 12 * 3 etc...

have a look here http://www.cprogramming.com/tutorial/lesson16.html
Last edited on
But If I put those lines it gives me the correct output.
Impossible. You're doing something else wrong.

EDIT: Oh, and there was an error in my previous post. It should have been -y-1, not just -y. I fixed it.
Last edited on
Topic archived. No new replies allowed.