- #1
Teh
- 47
- 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
Testing userBase = 4 and userExponent = 2
Expected output: 4^2 = 16
Your output: 4^2 = 4
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;
}
Expected output: 4^2 = 16
Your output: 4^2 = 4