67.7k views
0 votes
(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.4^2 = 16Given: #nclude 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;}

User Rashanna
by
5.0k points

1 Answer

4 votes

Answer:

Following are the code to this question:

RaiseToPower(baseVal, exponentVal-1);//calling method RaiseToPower and in second parameter we subtract value

resultVal = baseVal * RaiseToPower(baseVal, exponentVal-1);//defining resultVal variable that calculate baseVal and method value

Step-by-step explanation:

In the above-given code inside the "RaiseToPower" method is defined that accepts two parameters, which are "baseVal and exponentVal" and inside the method, an integer variable "resultVal" is defined, that holds a value that is 0.

  • In the next step, if the conditional statement is used, in if the block it checks the "exponentVal" variable value that is equal to 0, if it is true it assigns value 1 in "resultVal" otherwise it will go to else block in this block, the "resultVal" variable holds "baseVal" variable value and call the "RaiseToPower" method, and multiply the baseVal and method and store its value in resultVal and return the value.
  • Inside the main method, two integer variable userBase, userExponent is defined that holds a value and calls the above method and prints its return value.

please find the attachment of the code file and its output.

(C++) Write code to complete RaiseToPower(). Sample output if userBase is 4 and userExponent-example-1
User Jiawen
by
5.0k points