Writing a recursive math function

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

Discussion Overview

The discussion revolves around writing a recursive function to calculate the power of a number, specifically focusing on the implementation of the RaiseToPower() function in C++. Participants are examining the correctness of the code and identifying errors in the recursive logic.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their code and expresses difficulty in achieving the expected output for the RaiseToPower function.
  • Another participant points out a critical error in the recursive definition, questioning the correctness of the recursive call in the original code.
  • A different participant provides a corrected version of the RaiseToPower function, emphasizing the proper recursive definition and suggesting a change to test the function further.
  • One participant notes that the final return statement in their provided code is unreachable, indicating a redundancy in the logic.
  • Another participant acknowledges their mistake and expresses a willingness to learn from the feedback received.
  • A later reply reiterates the correction needed in the recursive call, reinforcing the proper structure of the function.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the original code's correctness, as multiple viewpoints on the recursive logic and structure are presented. The discussion includes both corrections and suggestions for improvement without a definitive resolution.

Contextual Notes

There are unresolved issues regarding the original recursive call and its implications for the function's output. The discussion highlights the importance of correctly defining the recursive relationship in mathematical functions.

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