-
recursion
aaggggg doing this with two variables is confusing. I can't figure out what to do next this is all I have:
Code:
void powers(int x, int y)
{
if(y+1==0)
return;
powers(y-1)
}
Please explain how do do this.
Edit: For negative powers how can I get it to ignore negative symbols.
-
What are you trying to do?
-
I assume your function should recursively calculate x to the power of y?
The recursive definition is:
x^y = 1 [if y==0]
x^y = x * x^(y-1) [otherwise]
Turn this into code!
Negative powers cannot be calculated by mere integers.
-
Well a function that did that would return a value and I see a void return type ;)
-
like this?
Code:
void powers(int x, int y)
{
if(y==0)
cout<<"1";
powers(x*x(y-1))
}
I plan to make two more functions. Two negatives=positive. One negative and one positive= negative. right?
-
Explain what you are trying to do first.
Are you trying to display the result of x raised to y? Are you trying to calculate and return it? Dealing with negative powers is easy as x raise to -2 is just 1 divided by x raise to 2.
-
I'm trying to get the result from x to the y power,
-
Ok for ease lets say that the domain of x is all integers and the domain of y is all positive integers.
Now we look at what x^y means. (For this post ^ means raised to the power of and not bitwise XOR).
x^y means that x is multipled by itself y times. It can also be written as
x * x^(y-1)
Well x^(y-1) can be written as
x * x^(y-2)
Well x^(y-2) can be written as
x * x^(y-3)
etc etc until you have
x * x^(y-y)
This is the essence of your recursive function.
The function header should be
Code:
int power (int x, unsigned y)
The basic flow is
Code:
If y is equal to 0
return 1
Else
return x times x^(y-1)
-
Try this. Not coded to test for accuracy. Otain base and exponent. If exponent not an integer probably best to use built in pow() function which allows both base and exponent to be doubles. Else declare a variable of type double called result which is assigned return value of function called for the first time. The function itself should therefore return a double. Declare a variable called temp which is of type double within each function call. If y == 0 you're done with function calls. else if y > 0 then y -=1; and if y < 0 then y += 1 for each successive function call. If y > 0 then temp = x * func(x, y) and if y < 0 then temp = (1/x) * func(x, y) and if y == 0 then temp = 1. Return temp from each function call to unwind the recursive function calls.
-
Isn't that quite a bit like what I had?
Is this right?
Code:
int powers(int x, unsigned y)
{
if(y==0)
cout<<"1";//any base to the power of 0 is 1
return 1;
powers(x*x(y-1))
}
-
I took out cout. It says x cannot be used as a function. How can I make it execute the powers function. If I put it in switch statement it has a syntax error.
In function 'int powers(int x, unsigned int)'
-
This ends the execution of your function. Are you sure you want to put this before you call powers()?
Do you mean to put
?
-
Is this any better?
Code:
int powers(int x, unsigned y)
{
if(y==0)
return 1;
else
powers(x*x, (y-1))
}
Edited!! again
What about my other questions?
-
No, you're still going to return 1 no matter what happens the first time you run the function. If you have multiple statements inside an if-statement, surround them with curly braces.
-
There is no need for the cout at all in this function. It calculates the value, it doesn't display it.
Also you need to work on your indentation.
Code:
int powers(int x, unsigned y)
{
if(y==0)
return 1;
else
return x * powers(x, y-1); /* x * x^(y-1) */
}
remember that if a function returns a value every branch needs to return a value