#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop /
float f(float x)
{
return pow(x,4)+2pow(x,3)+pow(x,2)-5;
}
int main(int argc, char *argv[])
{
double a,b;
double m,n,i,sum,root;
double esp=1e-8;
printf("Please enter the root interval[a,b]:");
scanf("%lf %lf",&a,&b);
i=0;
if(f(a)*f(b)<0)
{
while(b-a>esp)
{
m=(a+b)/2;
sum=f(m);
if (sum==0)
{
i++;
break;
}
else if (f(a)*f(m)<0)
{
m=b;
i++;
}
else
{
m=a;
i++;
}
}
}
else
{
printf("There are no roots in this interval.\n");
}
root=(a+b)/2;
printf("Number of iterations is %d\n",i);
printf("The approximate root of the equation is %lf\n",root);
system("PAUSE");
return 0;
}
C语音程序无法运行?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-sinJack 2021-11-10 13:18关注
#include <stdio.h> #include <stdlib.h> #include <math.h> float f(float x) { return pow(x,4)+2*pow(x,3)+pow(x,2)-5; } int main(int argc, char *argv[]) { double a,b; double m,n,i,sum,root; double esp=1e-8; printf("Please enter the root interval[a,b]:"); scanf("%lf %lf",&a,&b); i=0; if(f(a)*f(b)<0) { while(b-a>esp) { m=(a+b)/2; sum=f(m); if (sum==0) { i++; break; } else if (f(a)*f(m)<0) { m=b; i++; } else { m=a; i++; } } } else { printf("There are no roots in this interval.\n"); } root=(a+b)/2; printf("Number of iterations is %d\n",i); printf("The approximate root of the equation is %lf\n",root); system("PAUSE"); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 微信小程序协议怎么写
- ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
- ¥20 怎么用dlib库的算法识别小麦病虫害
- ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
- ¥15 java写代码遇到问题,求帮助
- ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看