Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve Programming Project 7, p.217, of Chapter 9 by creating a program named proj2_3.c . You must follow instructions and requirements described by this project,

image text in transcribedimage text in transcribedSolve Programming Project 7, p.217, of Chapter 9 by creating a program named proj2_3.c. You must follow instructions and requirements described by this project, meaning, your power function must implement as a recursive function using the formula image text in transcribedx n = (image text in transcribedx n 2)image text in transcribed2, if n is even, and image text in transcribedx n= image text in transcribedx(image text in transcribedx n 1), if n is odd. Zero credits for this program if its power function is the same as shown in section 9.6 Resursion, p.204.

Because the power function shown in section 9.6 Resursion, p.204, is int type, its return could overflow if your inputs for image text in transcribedx ncalculation results a too large value. For example, 10 for x and 9 for n results correctly while 10 for both x and n doesn't. So when you test your program, be sure you don't enter numbers that cause an overflow.

Like Program 1, we assume only valid integers, positive, zero, or negative, are entered here for x and n, so your program can skip input validation.

A demo of executable file is available for your testing: proj2_3.exe

Be sure your output display begins and ends with a blank line as seen in the demo.image text in transcribed

COOL 0 POLOTU, UGO UPIT 7. The power function of Section 9.6 can be made faster by having it calculate x" in a differ- ent way. We first notice that if n is a power of 2, then x" can be computed by squaring. For example, r* is the square of r, sor can be computed using only two multiplications instead of three. As it happens, this technique can be used even when n is not a power of 2. If n is even, we use the formula x = (^2). If n is odd, then x" =.xxx-'. Write a recursive func- tion that computes x". (The recursion ends when n=0, in which case the function returns 1.) To test your function, write a program that asks the user to enter values for x and n, calls power to compute x, and then displays the value returned by the function. 9.6 Recursion A function is recursive if it calls itself. For example, the following function com- putes n! recursively, using the formula n! = nx (n 1)!: int fact (int n) if (n C:\3515\Proj2\Proj2_solution> C:\3515 Proj2 Proj2_solution >proj2_3 Enter value for x: 5 Enter value for n: -2 5 raised to the power of 2 = 1 C:\3515\Proj2\Proj2_solution>proj2_3 Enter value for x: 9 Enter value for n: 0 raised to the power of 0 = 1 C:\3515 Proj2\Proj2_solution proj2_3 Enter value for x: 4 Enter value for n: -3 4 raised to the power of -3 = 1 C:\3515\Proj2\Proj2_solution> Negative value or zero for n always results 1 by Program 3 if it is implemented as specified

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

More Books

Students also viewed these Databases questions

Question

List the advantages and disadvantages of the pay programs. page 505

Answered: 1 week ago