Need help with this problem using the methods given. Write C++ programs to perfo
ID: 3870042 • Letter: N
Question
Need help with this problem using the methods given.
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems.
• power.cpp: Write a program that computes integer powers of floating-point numbers in two ways—recursively and interatively. You may not use the math library for this assignment.
Your code should implement the following two methods.
You must implement these function prototypes exactly as entered below. Your code must match the return type, argument types, and function name exactly (case-sensitive):
The following two methods take as input a double-precision floating point number and an exponent to raise it to and return the value of the base raised to the exponent.
– double recursivePow(double base, int exponent);
The important feature of this method is that it must solve this problem recursively.
– double iterativePow(double base, int exponent);
The important feature of this method is that it must solve this problem iteratively.
Explanation / Answer
Part 1: Recursive power function
double recursivePow(double base, int exponent){
if (exponent != 0)
return (base * recursivePow(base, exponent-1));
else
return 1;
}
Part 2: Iterative power function
double iterative Pow(double base, int exponent){
if (exponent != 0){
out=1
for(int i=1; i<=exponent; i++){
out = base * out;}
return out;}
else,
return 1;
}
main function:
int main(){
double base, result_R, result_I;
int exponent;
cout<<"Enter base: "<<endl;
cin>>base;
cout<<"Enter power(positive): "<<endl;
cin>>exponent;
result_R = recursivePow(base, exponent);
cout<<"using recursive method: "<<result_R<<endl;
result_I = iterativePow(base, exponent);
cout<<"using iterative method: "<<result_I<<endl;
}