8. Chapter 6, \'\'C Control Statements: Looping,\'\' (Listing 6.20) shows a powe
ID: 3760110 • Letter: 8
Question
8. Chapter 6, ''C Control Statements: Looping,'' (Listing 6.20) shows a power() function that returned the result of raising a type double number to a positive integer value. Improve the function so that it correctly handles negative powers. Also, build into the function that 0 to any power other than 0 is 0 and that any number to the 0 power is 1. (It should report that 0 to the 0 is undefined, then say it's using a value of 1.) Use a loop. Test the function in a program. 9. Redo Programming Exercise 8, but this time use a recursive function.Explanation / Answer
Hi,
Below is the solution to your problem:
8.Without recursion:
9.With recursion:
/* Program to calculate and use power() function using recursion
*/
#include<stdio.h>
float power(float x, int y)
{
float temp;
if((x==0)&&(y!=0))
return 0;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
{
if(y > 0)
return x*temp*temp;
else
return (temp*temp)/x;
}
}
/* Program to test function power */
int main()
{
float x = 2;
int y = -3;
printf("%f", power(x, y));
getchar();
return 0;
}
Hope that helps...HAPPY ANSWERING!!!!!!!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.