Question
Calculating Integer Powers Using a Loop with C++ Denition 1. A power is an exponent to which a given quantity is raised. The expression xn
Calculating Integer Powers Using a Loop with C++
Denition 1. A power is an exponent to which a given quantity is raised. The expression xn is therefore known as x to the nth power. The power may be an integer, real number, or complex number. Only consider powers that are integers. Do not use any standard Math library function. Use only basic arithmetic operators and a loop to compute the powers for the non-trivial cases, the last two rows of Table 1.This program should be able to compute any integer powers so it must cover all the cases shown in the table. There are some redundancies and overlap in the Table 1 but it covers all the cases for integer powers. Structure your code to avoid the redundancies and merge cases where possible. Write a program that computes the integer power of a real number. Use decision statements that cover all the cases that are given in Table 1. Prompt the user for the base, any real number, and exponent, an integer, of the power and then compute it. If the base is zero and exponent is less than or equal to zero, your program should indicate that the power is indeterminate by displaying nan (not a number), as shown in the sample run in Listing 1. All powers should be displayed to the nearest hundred thousandths (ve decimal places). More generally, the program interaction should be formatted as shown in the sample runs.
base(b) | exponent (n) | bn |
0 | n is less than or equal to 0 | indeterminate |
0 | n is greater than 0 | 0 |
b | 0 | 1 |
1 | n | 1 |
b | 1 | b |
b | -1 | 1/b |
-1 | n is even | 1 |
-1 | n is odd | -1 |
b | n is positive | b*b*b*...*b n factors |
b | n is negative | b*b*b*...*b n factors |
Table 1: Cases When Computing Integer Powers Listing 1: Sample Run 1 Enter the base and integer exponent of the power -> 0 -8 2 0.00000^-8 = nan Listing 2: Sample Run 1 Enter the base and integer exponent of the power -> 0 39 2 0.00000^39 = 0.00000 Listing 3: Sample Run 1 Enter the base and integer exponent of the power -> 2.95 0 2 2.95000^0 = 1.00000 Listing 4: Sample Run 1 Enter the base and integer exponent of the power -> 1 -4 2 1.00000^-4 = 1.00000 Listing 5: Sample Run 1 Enter the base and integer exponent of the power -> -2.865 1 2 -2.86500^1 = -2.86500 Listing 6: Sample Run 1 Enter the base and integer exponent of the power -> -2.5 -1 2 -2.50000^-1 = -0.40000 Listing 7: Sample Run 1 Enter the base and integer exponent of the power -> -1 200 2 -1.00000^200 = 1.00000 Listing 8: Sample Run 1 Enter the base and integer exponent of the power -> -1 1000001 2 -1.00000^1000001 = -1.00000 Listing 9: Sample Run 1 Enter the base and integer exponent of the power -> 2.5 5 2 2.50000^5 = 2.50000 x 2.50000 x 2.50000 x 2.50000 x 2.50000 = 97.65625 Listing 10: Sample Run 1 Enter the base and integer exponent of the power -> 2.5 -4 2 2.50000^-4 = 1.00000 / (2.50000 x 2.50000 x 2.50000 x 2.50000) = 0.02560
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