Thanks for all the help. I did see where I made it way to large. Learned
that x = abs(x); replaces:
if ( x < 0)
x = x * -1
and saw that the little thing which calculates pow will default to 1 so
didn't need the lines
if ( x == 0)
return 1;
Here is what I wound up with. I'm sure many see it as far too wordi and
long and spread out. I do this with malas and forthought. I fought tiny
little files while developing missles. I asked, if it is right why hide
it?
/* raises numbers to integer powers */
#include <stdio.h>
double power(double num, int exp);
int main(void)
{
double num, xpow;
int exp;
printf("Enter a number and the positive integer power to which\n"
"the power will be raised. Enter q to quit.\n");
while(scanf("%lf%d", &num, &exp) == 2)
{
xpow = power(num, exp);;
printf("%.3e to the power %d is %.3e\n", num, exp, xpow);
}
return 0;
}
double power(double num, int exp)
{
double pow = 1.0;
int i, b;
if ( num == 0 )
return 0;
b = abs(exp);
for( i=1; i <= b; i++)
pow = pow * num;
if ( exp < 0)
return 1 / pow;
else
return pow;
}
Best wishes
- Karl F. Larsen, 3310 East Street, Las Cruces,NM (505) 524-3303 -