c语言编程求谢啦 正在考试 急!!

来源:百度知道 编辑:UC知道 时间:2024/06/25 08:53:26
编写程序,用梯形法求一元函数f(x)=sin(2x)+3x,在区间[0,3.1416]上的积分近似值S,保留3位小数(小区间数n=10)。~14.804
编写程序,求下面数列前20项的和。结果保留2位小数。
-1,1/(1+3),-1/(1+3+5),...,(-1)^n/(1+3+5+...+(2n-1)),其中的^表示次方

1.
#include "stdio.h"
#include "math.h"

double f(double x){
return sin(2*x)+3*x;
}

int fuhetixing(){
double a=0,b=3.1416,h=b-a,t1,t2,s,x;
t1=(h/2)*(f(a)+f(b));
while (1){
s=0;
x=a+h/2;
do{
s+=f(x);
x+=h;
}
while(x<b);
t2=t1/2+(h/2)*s;
if(fabs(t2-t1)>=0.5e-7){
h=h/2;
t1=t2;
}
else{
printf("%0.3lf\n",t2);
return 1;
}
}
}

int main(){
fuhetixing();
}

2.
#include "stdio.h"
#include "math.h"

double f(int n){
int i;
double s=0;
for(i=1;i<=n;i++){
s+=pow(-1,i)/pow(i,2);
}
return s;
}

in