数値計算法レポート課題1(OK)


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. int i;
  6. double data,sum,mean,mean2,meansq,stdev,sum2,status;
  7. FILE *fpDATA;
  8.  
  9. int main(){
  10.  
  11. fpDATA = fopen("input.dat","r");
  12.  
  13. if(fpDATA==NULL){
  14. printf("input file open error !\n");
  15. printf("program terminated.\n");
  16. exit(0);
  17. }
  18.  
  19. i=0;
  20. sum=0;
  21. sum2=0;
  22. status = 0;
  23. data=0;
  24.  
  25.  
  26. while(fscanf(fpDATA, "%lf", &data) != EOF){
  27. sum = sum + data;
  28. sum2 = sum2 + data * data;
  29. i++;
  30. }
  31.  
  32. mean = sum/i;
  33. mean2 = sum2/i;
  34. meansq = mean * mean ;
  35. stdev = sqrt(mean2 - meansq);
  36.  
  37. printf("Number of data = %d\n",i);
  38. printf("Mean = %lf\n",mean);
  39. printf("Standard Deviation = %lf\n",stdev);
  40.  
  41. fclose(fpDATA);
  42.  
  43. }
  44. i=0;
  45. sum=0;
  46. sum2=0;
  47. status = 0;
  48.  
  49. while(status != EOF){
  50. status = fscanf(fpDATA, "%lf", &data);
  51. sum = sum + data;
  52. sum2 = sum2 + data * data;
  53. i++;
  54. }
  55.  
  56. mean = sum/(i-1);
  57. mean2 = sum2/(i-1);
  58. meansq = mean * mean ;
  59. stdev = sqrt(meansq - mean2);
  60.  
  61. printf("Number of data = %d\n",i-1);
  62. printf("Mean = %lf\n",mean);
  63. printf("Standard Deviation = %lf\n",stdev);
  64.  
  65. fclose(fpDATA);
  66.  
  67. }