Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
image text in transcribed

/* 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

Explain the process of MBO

Answered: 1 week ago