#include <stdio.h>
#define ID "/*?? 学生番号 */"
#define NAME "/*?? 名 前 */"
double fx(double);
double dabs(double, double);
void main() {
double a = 0.0; //積分区間の始点
double b = 10.0; //積分区間の終点
double e = 0.0000001; //収束判定定数
double d = e + 1.0; //積分値比較用
double s1 = 0.0; //積分値格納用(1つ前)
double s2 = 1.0; //積分値格納用
int n = 0; //等分数(実際の等分数は2n)
printf("%s %s\n\n", ID, NAME);
do {
int i;
double h; //区間幅
double yo; //奇数番目の分点における関数値
double ye; //偶数番目の分点における関数値
n++;
s1 = s2;
h = (b-a)/n; /*?? (1) ここに区間幅を求める式を記述する */
ye = 0.0;
yo = 0.0;
/*?? (2) yoを求めるためのforの条件を記述する */
for (i = 1; i<=n; i+=2) {
//for( i = 0; i <= n; i++)ではないので注意
yo = yo + fx(a+i*h);
}//end for
/*?? (3) yeを求めるためのforの条件を記述する */
for (i = 2; i<=n; i+=2) {
//for( i = 0; i <= n; i++)ではないので注意
ye = ye + fx(a+i*h);
}//end for
s2 = (h/3.0)*(fx(a)+fx(b)+4.0*yo+2.0*ye);
if (n > 1) {
d = dabs(s1, s2);
printf("n=%3d, S1=%12.10f, S2=%12.10f, d=%12.10f\n", n, s1, s2, d);
}//end if
} while(d >= e);
printf("n=%3d, S=%12.10f, d=%12.10f\n", n, s2, d);
}//end main
double fx(double x) {
/*?? (4) ここに関数f(x)の値を返す式を記述する*/
//クラスで関数が違うので注意
return f(x);
}//end fx
double dabs(double d1, double d2) {
if (d1 > d2) return d1 - d2;
else return d2 - d1;
}//end dabs