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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top