第一范文网 - 专业文章范例文档资料分享平台

c语言上机实验题

来源:用户分享 时间:2025/5/26 2:00:14 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

}

25.用牛顿迭代法求方程3x3-3x2+x-1=0在x0=2附近的实根。要求: (1)用函数float newtoon(float x)求方程在x附近的根;

(2)用函数float F(float x)求x处的函数值,用函数float F1(float x)求f(x)在x处的导数; (3)在主函数中输入x0,调用函数求得方程的近似根(精度要求为10-5),并输出结果。 请完善下列程序,使之完成上述功能。并请以注释的方式在程序的最后给出你在运行该程序时所选用的测试数据及在该测试数据下的运行结果。

【源程序】

#include #include #include float F(float x) #include { float F(float x) return 3*x*x*x-3*x*x+x-1; { } return 3*x*x*x-3*x*x+x-1; float F1(float x) } { float F1(float x) return __________________; { } return 9*x*x-6*x+1; float newtoon(float x) } { float newtoon(float x) float f,f1,x0; { do { ______________; float f,f1,x0; f=F(x0); do { x0=x; f1=F1(x0); f=F(x0); x=______________; f1=F1(x0); }while(_______________); x=x0-f/f1; return x; }while(fabs(x-x0)>1e-5); } return x; void main() } { void main() float x0; { scanf(\ float x0; printf(\result =%.2f\\n scanf(\\ printf(\.2f\\n \

} getch();

} 测试数据:2↙

运行结果:The result =1.00

26.请设计程序,用牛顿迭代法求f(x)=cos(x)-x的近似根,要求精确到10-6。

#include #include

float F(float x) {

return cos(x)-x; }

float F1(float x) {

return -sin(x)-1; }

float newtoon(float x) {

float f,f1,x0; do { x0=x; f=F(x0); f1=F1(x0); x=x0-f/f1;

}while(fabs(x-x0)>1e-6); return x; }

void main() {

float x0;

scanf(\

printf(\ getch(); }

27.已知f(x)=lnx+x2在(1/e, 1)内有唯一的一个实根。请设计程序,用二分法求该近似实根。精确到|f(x)|<0.0001为止。 #include #include float F(float x) {

return log(x)+x*x; } main()

{ float m,n,l,a,b,c,x; a=exp(-1);b=1; m=F(a); n=F(b); do

{ c=(a+b)/2 ; l=F(c); if(m*l<0) b=c;

else a=c;

}while(fabs(l)>=0.0001); x=c;

printf(\ getch(); }

28.请设计程序,用弦截法求程2x3-4x2+3x-6=0在(0,3)内的近似根,直到|f(x)|<0.0001为止。

#include #include float F(float x) {

return 2*x*x*x-4*x*x+3*x-6; } main() {

float a=0,b=3,m,n,l,x; m=F(a); n=F(b); do

{ x=(a*n-b*m)/(n-m); l=F(x); if(l*m>0) a=x; if(l*n>0) b=x;

}while(fabs(l)>=1e-4); printf(\ getch(); }

29.请编辑调试下列程序,观察其运行结果,理解数组名作为函数参数时的作用,并描述程序实验的功能。 【源程序】

#include int fun(int a[],int b[]) {

int i,j=0;

for(i=0;a[i];i++)

{ if(i%2==0)continue ; if(a[i]>10)

b[j++]=a[i]; }

return j; }

void main() {

int a[10]={3,15,32,23,11,4,5,9},b[10]; int i,x; x=fun(a,b) ;

for(i=0;i

printf(\ printf(\}

30.以下程序的功能是从键盘上输入10个整数,并检测整数3是否包含在这些数据中,若包含3,则显示出第一个3出现的位置,程序有些错误,试改正之。 【源程序】

#include #include void main() void main() { { int data[10]; int data[10],j; j=0; j=0; while (j<10) while (j<10) { scanf(\ { scanf(\ j++; j++; } } for(j=0;j<10;j++) for(j=0;j<10;j++) if(data[j]=3) if(data[j]==3) { printf(\is in the position of %d\\n { printf(\is in the position of %d\\n \\ continue; break; } } if(j==10) if(j==10) printf(\ printf(\} getch(); } 31.用以下公式求数列a0,a1,…,a19 a0=0; a1=1; a2=1;

ai= a i-3+ 2ai-2+ ai-1 当 i 大于2时

#include void main()

搜索更多关于: c语言上机实验题 的文档
c语言上机实验题.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8z5ap9wwtp99g5n14c0q_6.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top