hqt

Workshop 8

hqt
Jul 25th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAX 100
  3.  
  4. int main(){
  5.     int sum = 0;
  6.     int element[MAX];
  7.     int i = 0;
  8.     char filename[80];
  9.  
  10.     printf("Statistics Calculator\n");
  11.     printf("=====================\n");
  12.  
  13.     printf("Enter the name of the data file: "); scanf("%s",filename);
  14.     FILE * f;
  15.     f = fopen(filename, "r");
  16.     if (f == NULL){
  17.         printf("Cannot open file\n");
  18.         return;
  19.     }
  20.     while (!feof(f)){
  21.         fscanf(f,"%d",&element[sum++]);
  22.     }
  23.     fclose(f);
  24.  
  25.     float m = 0.0;
  26.     float s = 0.0;
  27.     for(i=0; i<sum; i++){
  28.         m += element[i];
  29.         s += element[i]*element[i];
  30.     }
  31.     m/=sum;
  32.     float res = s/sum - m*m;
  33.  
  34.     printf("The number of data values read from this file was %d\n", sum);
  35.     printf("Their statistical mean is %f\n", m);
  36.     printf("Their standard deviation is %f\n", res);
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment