-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex09_08.c
62 lines (54 loc) · 1.47 KB
/
ex09_08.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
61
62
/*
* pr_pset09_08:
*
* Improve the power() (Listing 6.20) 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 <stdbool.h>
double power(double base, int exponent);
int main(void)
{
double base, number;
int exponent;
printf("*** Test power() function with loop. ***\n");
printf("Enter a number and the power (q to quit): ");
while (scanf("%lf %d", &base, &exponent) == 2)
{
number = power(base, exponent);
if (base == 0 && exponent == 0)
;
else
printf("%g to the %d power is %g\n",
base, exponent, number);
printf("Enter next values (q to quit): ");
}
printf("Bye.\n");
return 0;
}
double power(double base, int exponent)
{
double power = 1;
bool exponent_is_negative = false;
if (base == 0 && exponent == 0)
{
printf("Warning: 0 to 0 power is not a number.\n");
return 1;
}
else
{
if (exponent < 0)
{
exponent_is_negative = true;
exponent = -exponent;
}
for (int i = 1; i <= exponent; i++)
power *= base;
if (exponent_is_negative)
power = 1.0 / power;
}
return power;
}