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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top