fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define nmax 100
  5. #define pi atan(1.0)*4.0
  6. #define xa 1.0 // 積分範囲_下限
  7. #define xb exp(2) // 積分範囲_上弦
  8. #define h 0.5 // 刻み幅
  9.  
  10. double f( double );
  11. double Sdai( int ni,double y[] );
  12.  
  13. int main(void)
  14. {
  15. double S,Sd,Err_d;
  16. double xt[nmax],yt[nmax];
  17. int i,j,l,ni;
  18.  
  19. /** きざみ幅 **/
  20. ni=(xb-xa)/h;
  21.  
  22. /** 初期値の代入 **/
  23. for(i=0; i<=ni; i++){
  24. xt[i] = xa + i*h;
  25. yt[i] = f(xt[i]);
  26. printf("%12.9f, %12.9f \n", xt[i], yt[i]);
  27. }
  28.  
  29. /** 面積の計算 **/
  30. S=6*(pow(exp(1),2)-(1/4)pow(exp(1),4)+1/4); // 真値
  31. Sd=Sdai(ni,yt);
  32. Err_d=fabs(Sd-S);
  33.  
  34. /** 計算結果の出力 **/
  35. printf(" きざみ幅 真値 台形 \n");
  36. printf("%12.9f %12.9f %12.9f %12.9f \n",h,S,Sd,Err_d);
  37. }
  38.  
  39. /** 積分関数 **/
  40. double f(double xx)
  41. {
  42. return 6*x*log(x);
  43. }
  44.  
  45. /** 台形公式による積分 **/
  46. double Sdai(int ni,double y[])
  47. {
  48. double S;
  49. int l;
  50.  
  51. S=0.0;
  52. for(l=1; l<ni; l++) S+=2.0*y[l];
  53. S=(S+y[0]+y[ni])*h/2.0;
  54. return S;
  55. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:30:27: error: expected ')'
  S=6*(pow(exp(1),2)-(1/4)pow(exp(1),4)+1/4); // 真値
                          ^
prog.c:30:7: note: to match this '('
  S=6*(pow(exp(1),2)-(1/4)pow(exp(1),4)+1/4); // 真値
      ^
prog.c:42:12: error: use of undeclared identifier 'x'
  return 6*x*log(x);
           ^
prog.c:42:18: error: use of undeclared identifier 'x'
  return 6*x*log(x);
                 ^
3 errors generated.
stdout
Standard output is empty