未分类题

下列给定程序中,函数fun()的功能是:用下面的公式求π的近似值,直到最后一项的绝对值小于指定的数(参数num)为止。
π/4≈1-1/3+1/5-1/7+…
例如,程序运行后,输入0.0001,则程序输出3.1414。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <math.h>
include <stdio.h>
float fun(float num)
{ int s;
float n,t, pi;
t=1 ;pi=0; n=1; s=1;
/**************found***************/
while (t>=num)
{
pi=pi+ t;
n=n+2;
s=-s;
/**************found**************/
t=s% n;
}
pi=pi*4;
return pi;
}
main ()
{ float n1, n2;
clrscr ();
printf ('Enter a float number: ');
scanf ('%f', &n1);
n2=fun (n1);
print f (' %6.4 f/n ', n2);
}

A.0001,则程序输出3.1414。
B.h>
C.h>
D.4

【参考答案】

(1)错误:while(t>=num) 正确:while (fabs(t)>=num)(2)错误;t=s %n; 正确:......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

未分类题
听力原文:Now listen, and fill in the spaces.Man: Good morning. Marketing Department. Peter Menzies speaking.Woman: Hello. Could I speak to John Fitzgerald, please?Man: I'm afraid he's not in the office at the moment.Woman: Well, this is Elizabeth Parnell calling. I wanted to talk to John about the meeting next week. You see, I only get back on Tuesday night from a trade fair in the States.Man: So, would you like me to give him a message?Woman: Yes, could you ask him if we can postpone Wednesday's meeting? Till after lunch. That would be easier. I was originally going to be at ten A.m.Man: OK. I'll ask him to change it. I'll get back to you with a timE.Woman: Thanks. And could you also ask him to add another item for discussion at the meeting? I thought we were going to talk about the revised budgets – but I can't see this on the agendA.Man: OK. I'd better ask him to call you ...Woman: Yes, pleasE.I'm at Head Office at the moment. Can he phone me here today – I won't be back at my own desk until tomorrow afternoon.Man: Right, I'll give him the messagE.Woman: Thanks.Man: ByE.Woman: GoodbyE.Look at the note below.You will hear a woman calling about the arrangements for a meeting.TELEPHONE MESSAGEFor: John FitzgeraldName of caller: Elizabeth ParnellDate: 7 12 2002Time: 10.05Message: Elizabeth will be in the USA untilnext Tuesday, for a (9)______Could you:Change the meeting to (10)______ on Wednesday.Add an item to the agenda: the (11)______ ?Please phone Elizabeth today at (12)______
A.Man:
B.
Woman:
C.
Man:
D.m.
Man:
E.
Woman:
F.
Man:
G...
Woman:
H.
Man:
I.
Woman:
J.
Man:
K.
Woman:
L.
Look
M.
You
N.
TELEPHONE
O.05
Message:
P.
Add