-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise08.c
60 lines (47 loc) · 1.31 KB
/
exercise08.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// C Primer Plus
// Chapter 9 Exercise 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.
#include <stdio.h>
#include <stdlib.h> // prototype for abs()
double power(double base, int exponent);
int main(void)
{
double base, output;
int exponent;
printf("Test power() function:\n");
printf("Enter a :double: base and :int: exponent: ");
while (scanf("%lf %d", &base, &exponent) == 2)
{
output = power(base, exponent);
printf("%f ^ %d = %f \n", base, exponent, output);
printf("Enter a :double: base and :int: exponent: ");
}
return 0;
}
double power(double base, int exponent)
{
double power = base;
if (base == 0)
{
if (exponent == 0)
{
printf("Warning: 0 ^ 0 is undefined. Using 1.\n");
return 1.0;
}
else
return 0;
}
for (int i = 1; i < abs(exponent); i++)
{
power *= base;
}
if (exponent < 0)
power = 1 / power;
return power;
}