Question
Raising a number to integer power Write a C/C++ function that calculates the integer power of a number. The function should accept two parameters, the
Raising a number to integer power
Write a C/C++ function that calculates the integer power of a number. The function should accept two parameters, the base number of real value and the power (exponent) of integer value, and return the result.
To test your function use the main() function given bellow. Do not modify the main function. Your job is to define the function my_pow() and run (execute) the program to check that it works correctly.
The my_pow() function has similar functionality with the pow() function which is readily available by C++. However, you are NOT allowed to use the C++ pow() function in your solution. You should come up with a way to calculate the result using the knowledge that
/* This program cacluates the result of a number raised to an integer power. */
#include
using namespace std;
//Function declaration
double my_pow(double, int);
int main()
{
double base;
int power;
//Continue asking for input until user enters 0 as base.
do
{
cout cin >> base;
if (base == 0)
break;
cout cin >> power;
cout }
while (true);
system("pause");
return 0;
}
//Function definition
(Add your code here)
Sample input/output:
Enter the base of the number (0 to exit): 3
Enter the power (integer): 3
The result is: 27
Enter the base of the number (0 to exit): 4
Enter the power (integer): 0
The result is: 1
Enter the base of the number (0 to exit): -5
Enter the power (integer): 3
The result is: -125
Enter the base of the number (0 to exit): 2.34
Enter the power (integer): 2
The result is: 5.4756
Enter the base of the number (0 to exit): 2
Enter the power (integer): -3
The result is: 0.125
= = ..., e.g. exponent or power number or base 103 10x10x10 = 1,000 Your function should be also able to handle zero and negative powers. E.g. 5 1, 2 1/(2x2x2) = 0.125 23=1/(2x2x2)=0.125
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started