求任意数的幂,请问哪里错了?
我刚学,自己按题目要求编的,用子函数实现,不过底数是整数时可以得到正确的答案.
当底数不是整数就得不到正确答案,比如,1.5的三次方就得3.000000.请高手指教~~~
#include "stdio.h "
power(float x, int n)
{ int i;
float y;
y=1;
for(i=1;i <=n;i++)
y=y*x;
return y;
}
main()
{float a,c;
int b;
printf( "please input the down and the exponent:\t ");/*输入底数和指数*/
scanf( "%f,%d ",&a,&b);
c=power(a,b);
printf( "the result is :%f ",c);
}
[解决办法]
楼主兄弟...你要把你的函数定义一个返回值啊...如下:
float power(float x, int n)