home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 193_01 / fstat.c < prev    next >
Text File  |  1985-11-14  |  5KB  |  203 lines

  1. /*    
  2. ** fstat.c    File Statistics Program        by F.A.Scacchitti 10/8/85
  3. **
  4. **        Written in Small-C Version 2.10 or later
  5. **
  6. **        Scans file and displays distribution of 
  7. **        characters.
  8. **        Calculates and displays mean, mode, median 
  9. **        and range of file.
  10. **        Displays histogram of distribution.
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15. #define BUFSIZE 16384
  16.  
  17. int fdin;    /* file  pointer */
  18. int i, j, temp, value, count, total, *file, *sorted;
  19. int sum, hisum, meansum, himeansum, mean, eflag, changing;
  20. int median, oddmedian, range, min, max, mode;
  21. int *data, scale;
  22. char c, *inbuf;
  23.  
  24. main(argc,argv) int argc, argv[]; {
  25.  
  26.    if(argc < 2) {
  27.       printf("\nfstat usage: fstat <input file>\n");
  28.       exit();
  29.    }
  30.    if((fdin = fopen(argv[1],"r")) == NULL) {
  31.       printf("\nUnable to open file %s\n",argv[1]);
  32.       exit();
  33.    }
  34.  
  35.    inbuf = calloc(BUFSIZE,1);
  36.    file = calloc(256,2);
  37.    sorted = calloc(256,2);
  38.    data = calloc(17,2);
  39.    eflag = FALSE;
  40.    sum = hisum = meansum = himeansum = mean = mode = j = 0;
  41.  
  42.    printf("reading the file-");
  43.  
  44.    do {
  45.       count = read(fdin,inbuf,BUFSIZE);
  46.  
  47.       for(i=0; i< count; i++){
  48.  
  49.          value = inbuf[i];
  50.  
  51.                         
  52.          if(value < 0)
  53.             value = 256 + value;
  54.          file[value]++;
  55.          if(++sum == 10000){
  56.             hisum++;
  57.             sum =0;
  58.          }
  59.          if((meansum += value) >= 10000){
  60.             himeansum++;
  61.             meansum -= 10000;
  62.          }
  63.       }
  64.    } while(count == BUFSIZE);
  65.  
  66. /*
  67. ** Calculate the mean
  68. */
  69.  
  70.    printf("calculating mean-");
  71.  
  72.    do{
  73.       if((meansum -= sum) < 0)
  74.          if(himeansum > 0){
  75.             himeansum--;
  76.             meansum += 10000;
  77.          }else{
  78.             meansum += sum;
  79.             eflag = TRUE;
  80.             mean--;
  81.          }
  82.       if((himeansum -= hisum) < 0){
  83.          himeansum += hisum;
  84.          eflag = TRUE;
  85.       }else{
  86.          mean++;
  87.       }
  88.    }while(eflag == FALSE);
  89.  
  90. /*
  91. ** Calculate range, find mode min and max, fill the sorted array
  92. */
  93.  
  94.    printf("calculating range-");
  95.  
  96.    min = max = file[0];
  97.  
  98.    for(i = 0; i <= 255; i++){
  99.       sorted[i] = file[i];
  100.       if(file[i] > max){
  101.          max = file[i];
  102.          mode = i;
  103.       }
  104.       if(file[i] < min)
  105.          min = file[i];
  106.    }
  107.    range = max - min + 1;
  108.  
  109. /*
  110. ** Sort the sorted array to calculate median
  111. */
  112.  
  113.    printf("sorting the array");
  114.  
  115.    changing = TRUE;
  116.  
  117.    while(changing){
  118.       changing = FALSE;
  119.       for(i = 0; i <= 254; i++)
  120.          if(sorted[i] > sorted[i+1]){
  121.             temp = sorted[i];
  122.             sorted[i] = sorted[i+1];
  123.             sorted[i+1] = temp;
  124.             changing = TRUE;
  125.          }
  126.    }
  127.  
  128.    median = (sorted[128] + sorted[127]) / 2;
  129.    oddmedian = (sorted[128] + sorted[127]) % 2;
  130.  
  131. /*
  132. ** Display the results
  133. */
  134.  
  135.    printf("\n    0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F\n");
  136.  
  137.    for(i = 0; i <= 255; i++) {
  138.       printf("%5d", file[i]);
  139.       chkkbd();
  140.    }
  141.  
  142.    printf("\n %d%04d characters read from file %s\n", hisum, sum, argv[1]);
  143.    printf("file mean = %d  ",mean);
  144.    if((himeansum || meansum) > 0)
  145.       printf("%d%04d/%d%04d",himeansum,meansum,hisum,sum);
  146.    printf("         mode = %d  ( %x hex)", mode, mode);
  147.    printf("\n");
  148.    printf("file median = %d", median);
  149.    if(oddmedian)
  150.       printf(" 1/2 ");
  151.    else
  152.       printf("     ");
  153.    printf("    file range = %d  [ min = %d    max = %d ]\n", range, min, max,);
  154.    printf("\nDepress spacebar to display histogram ");
  155.    getchar();
  156.  
  157. /*
  158. ** Sum the data in 16 groups of 16 elements and find max. value
  159. */
  160.    max = 0;
  161.    for(i = 1; i <= 16; i++){
  162.       for(j = 0; j <= 15; j++)
  163.          data[i] += file[(i - 1) * 16 + j];
  164.       if(data[i] > max)
  165.          max = data[i];
  166.    }
  167. /*
  168. ** Calculate scaling for plot
  169. */
  170.    scale = max / 50;
  171.    temp = max % 50;
  172.    if(temp / scale > 7)
  173.       scale++;
  174.    printf("   scale = %4d\n\n", scale);
  175.  
  176. /*
  177. ** Print data and plot of histogram
  178. */
  179.  
  180.    for(i = 0; i <= 15; i++){
  181.       printf(" %3d to %3d = %5d ||",i  * 16, (i * 16) + 15, data[i + 1]);
  182.       temp = data[i + 1] / scale;
  183.       if(data[i + 1] % scale > 0)
  184.          temp++;
  185.       while(temp-- > 0)
  186.          printf("*");
  187.       printf("\n");
  188.    }
  189.  
  190. /*
  191. ** close up shop
  192. */
  193.    fclose(fdin);
  194. }
  195.  
  196. chkkbd(){ char c;
  197.  
  198.    if((c = bdos(6,255)) == 19)     /* hold on ^S */
  199.       if((c = getchx()) == 3)
  200.          exit();                    /* exit on ^C */
  201. }                                   /* continue   */
  202.  
  203.