Writing a recursive math function

In summary, the code is missing a return statement in the else block of the conditional in the recursive call.
  • #1
Teh
47
0
Is my code >.> I tried my best trying to solve but could not get the right answer for my program. I would like know what i did wrong.

Write code to complete RaiseToPower(). Sample output if userBase is 4 and userExponent is 2 is shown below. Note: This example is for practicing recursion; a non-recursive function, or using the built-in function pow(), would be more common.

4^2 = 16
Code:
#include <iostream>
using namespace std;

int RaiseToPower(int baseVal, int exponentVal){
   int resultVal = 0;

   if (exponentVal == 0) {
      resultVal = 1;
   }
   else {
      resultVal = baseVal * RaiseToPower(baseVal  , exponentVal - 2 ) ; /*my program 8 */
   }

   return resultVal;
}

int main() {
   int userBase = 0;
   int userExponent = 0;

   userBase = 4;
   userExponent = 2;
   cout << userBase << "^" << userExponent << " = "
        << RaiseToPower(userBase, userExponent) << endl;

   return 0;
}
Testing userBase = 4 and userExponent = 2
Expected output: 4^2 = 16
Your output: 4^2 = 4
 
Technology news on Phys.org
  • #2
There's a critical error on your recursive line. The recursive definition of exponent is given by:

\(\displaystyle {a}^{n} = a * {a}^{?}\)

What's the ? supposed to be and how does that compare with your recursive definition?
 
  • #3
Here's an interesting piece of code that gets the job done:

Code:
int RaiseToPower(int baseVal, int exponentVal) {
   
   if (exponentVal == 0)
      return 1;
   else 
      return (baseVal * RaiseToPower(baseVal, exponentVal - 1));
	
   return 1;
}

Try changing the '1' in the last [m]return[/m] to a '5' and see what happens!

Thanks to FallArk.
 
  • #4
greg1313 said:
Here's an interesting piece of code that gets the job done:

Code:
int RaiseToPower(int baseVal, int exponentVal) {
   
   if (exponentVal == 0)
      return 1;
   else 
      return (baseVal * RaiseToPower(baseVal, exponentVal - 1));
	
   return 1;
}

Try changing the '1' in the last [m]return[/m] to a '5' and see what happens!

Thanks to FallArk.

In your code that final return is redundant. You either go into the if or the else block of the conditional and both have a return value so that final return 1; is unreachable code. In the OPs version of the code there are no return statements other than a final return at the end of the function which returns the value set in one of the two branches of the conditional. The OPs problem is still with the recursive call in the else block.
 
  • #5
Hmm ... I must have made an error somewhere. Ah, well, live and learn. Thanks.
 
  • #6
You were close. The code should read:

resultVal = baseVal * raiseToPower(baseVal, exponentVal - 1 );

Hope this helps.
 

1. What is a recursive math function?

A recursive math function is a function that calls itself repeatedly until it reaches a base case. This is a common programming technique used to solve problems that can be broken down into smaller subproblems.

2. How do I write a recursive math function?

To write a recursive math function, first identify the base case(s) and the recursive case(s). The base case is the simplest version of the problem that can be solved without further recursion. The recursive case is the part of the problem that can be broken down into smaller subproblems. Then, use the base case to end the recursion and use the recursive case to call the function again with a smaller version of the problem. Repeat this process until the base case is reached.

3. What are the advantages of using a recursive math function?

Recursive math functions can be more elegant and concise compared to iterative solutions. They also allow for easier problem solving and code maintenance as they break down complex problems into smaller, more manageable parts.

4. Are there any disadvantages to using a recursive math function?

One potential disadvantage of recursive math functions is that they can be less efficient compared to iterative solutions. Each recursive call adds to the call stack, which can lead to performance issues if the function is called too many times. Additionally, recursive functions can be more difficult to debug and may be prone to stack overflow errors if not implemented correctly.

5. How do I determine when to use a recursive math function?

Recursive math functions are best used for problems that can be broken down into smaller subproblems and have a clear base case. They are often useful for working with data structures such as trees and graphs. However, it's important to consider the efficiency and potential limitations of using a recursive solution before implementing it.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
1
Views
987
  • Programming and Computer Science
Replies
3
Views
725
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
4
Views
781
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top