Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c++, Write code to complete RaiseToPower(). Sample output if userBase is 4 an

ID: 3590525 • Letter: I

Question

In c++, 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.

#include <iostream>
using namespace std;

int RaiseToPower(int baseVal, int exponentVal){
int resultVal = 0;

if (exponentVal == 0) {
resultVal = 1;
}
else {
resultVal = baseVal * /* Your solution goes here */;
}

return resultVal;
}

int main() {
int userBase = 0;
int userExponent = 0;

userBase = 4;
userExponent = 2;
cout << userBase << "^" << userExponent << " = "
<< RaiseToPower(userBase, userExponent) << endl;

return 0;
}

Explanation / Answer

If you want only the solution for 4^2=16

then here is your program

#include <iostream>
#include <cmath> eeds to be defined for pow()
using namespace std;
int RaiseToPower(int baseVal, int exponentVal){
int resultVal = 0;
if (exponentVal == 0) {
resultVal = 1;
}
else {
resultVal = pow(baseVal, exponentVal); \ code added
  
}
return resultVal;
}
int main() {
int userBase = 0;
int userExponent = 0;
userBase = 4;
userExponent = 2;
cout << userBase << "^" << userExponent << " = "
<< RaiseToPower(userBase, userExponent) << endl;
return 0;
}

and if you want RaiseToPower() for any number (may be float), then

here is your program

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float baseVal, exponentVal, resultVal;


cout << "Enter the base: ";
cin >>baseVal;
cout << "Enter the exponent: ";
cin >> exponentVal;

resultVal = pow(baseVal, exponentVal);
cout << baseVal << "^" << exponentVal << "=" << resultVal;

return 0;
}

outpu:

Enter the base: 2

Enter the exponent: 3

2^3=8 (your answer)

I haven't used int RaiseToPower(int baseVal, int exponentVal) in second program because it cant' be defined under int main(), and also there is no need of using int RaiseToPower(int baseVal, int exponentVal) in the second program, so i removed it.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote