问答题
编写fun()函数,它的功能是:利用以下所示的简单迭代方法求方程cos(y)-y=0的一个实根。
yn+1=cos(yn)
迭代步骤如下:
(1)取y1初值为0.0;
(2)y0=y1,把y1的值赋给y0;
(3)y1=cos(y0),求出一个新的y1;
(4)若y0-y1的绝对值小于0.000001,则执行步骤(5),否则执行步骤(2);
(5)所求y1就是方程cos(y)-y=0的一个实根,作为函数值返回。
程序将输出结果Result=0.739085。
请勿改动main()函数与其他函数中的任何内容,仅在函数fun()的花括号中填入所编写的若干语句。
部分源程序给出如下。
试题程序:
#include<conio.h>
#include<math.h>
#include<stdio.h>
float fun()
{
}
void main()
{
FILE *out;
float cos=fun();
printf("Result=%f\n", cos);
out=fopen("outfile.dat", "w");
fprintf(out, "%f", cos);
fclose(out);
}
【参考答案】
float y1=0.0, y0;
do
{//进行迭代
y0=y1;
y1=c......
(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)