MHB Writing a recursive math function

  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function Writing
AI Thread Summary
The discussion focuses on resolving issues in a recursive function designed to calculate the power of a number. The original code fails to produce the correct output because the recursive call incorrectly decrements the exponent by 2 instead of 1. The correct recursive definition states that a^n = a * a^(n-1). The suggested solution includes modifying the recursive line to decrement the exponent by 1, ensuring the function correctly computes the power. Additionally, the final return statement in the original code is identified as redundant since both branches of the conditional already return values. The corrected code snippet provided demonstrates the proper implementation of the recursive logic needed to achieve the expected output.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top