Writing a recursive math function

  • Context: MHB 
  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function Writing
Click For Summary
SUMMARY

The discussion centers on correcting a recursive function for calculating powers in C++. The original implementation of the function RaiseToPower() incorrectly decremented the exponent by 2 instead of 1, leading to incorrect results. The corrected version uses return (baseVal * RaiseToPower(baseVal, exponentVal - 1)); to properly compute the power. Additionally, the final return statement in the original code was identified as unreachable, indicating a need for code optimization.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Knowledge of recursion in programming
  • Familiarity with function return values
  • Basic mathematical concepts of exponentiation
NEXT STEPS
  • Study C++ recursion techniques and best practices
  • Learn about debugging and optimizing C++ code
  • Explore the use of built-in functions like pow() in C++
  • Practice writing recursive functions for different mathematical operations
USEFUL FOR

C++ developers, computer science students, and anyone interested in mastering recursion and optimizing mathematical functions in programming.

Teh
Messages
47
Reaction score
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
There's a critical error on your recursive line. The recursive definition of exponent is given by:

$${a}^{n} = a * {a}^{?}$$

What's the ? supposed to be and how does that compare with your recursive definition?
 
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.
 
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.
 
Hmm ... I must have made an error somewhere. Ah, well, live and learn. Thanks.
 
You were close. The code should read:

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

Hope this helps.
 

Similar threads

Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K