home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 161_01 / groups.c < prev    next >
C/C++ Source or Header  |  1985-08-29  |  2KB  |  89 lines

  1. /* groups - make groups of similar timings
  2.  */
  3. #include <stdio.h>
  4. #define ABS(n) ((n) < 0 ? (-n) : (n))
  5. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  6. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  7. #define NGROUPS 25
  8. #define GI groups[i]
  9. struct
  10.     {
  11.     short n;
  12.     double tmin, tmax;
  13.     double total;
  14.     short members[50];
  15.     } groups[NGROUPS] = {0};
  16. short igroup[NGROUPS] = {0};
  17. short itemp = 0;
  18. double gtemp = 0;
  19. char s[100][24] = {0};
  20. short ns = 0;
  21. double t = 0;
  22. short i = 0;
  23. short j = 0;
  24. short itmax = 0;
  25. double avg = 0;
  26. int ret = 0;
  27. char *fround();
  28. main()
  29.     {
  30.  
  31.     for (i = 0; i < NGROUPS; ++i)
  32.         GI.tmin = 9e19;
  33.     while (getchar() != '\n')
  34.         ;    /* skip heading line */
  35.     for (ns = 0; (ret = scanf("%22c%*d%lf", s[ns], &t)) != EOF; ++ns)
  36.         {
  37.         while (getchar() != '\n')
  38.             ;
  39. #if 0
  40.         printf("%22.22s %8.2f\n", s[ns], t);
  41. #endif
  42.         if (ret < 2)
  43.             printf("bad data!!\n");
  44.         for (i = 0; ; ++i)
  45.             {
  46.             if (GI.n != 0)
  47.                 avg = GI.total / GI.n;
  48.             if (GI.n == 0 ||
  49.                     (GI.tmin <= t && t-GI.tmin < GI.tmin / 3) ||
  50.                     (t <= GI.tmax && GI.tmax-t < t / 3))
  51.                 {
  52.                 GI.total += t;
  53.                 GI.tmin = MIN(GI.tmin, t);
  54.                 GI.tmax = MAX(GI.tmax, t);
  55.                 GI.members[GI.n] = ns;
  56.                 ++GI.n;
  57.                 break;
  58.                 }
  59.             }
  60.         }
  61.     for (i = 0; GI.n != 0; ++i)
  62.         {
  63.         igroup[i] = i;
  64.         itmax = i;
  65.         }
  66.     for (i = 0; i <= itmax; ++i)
  67.         {
  68.         itemp = igroup[i];
  69.         gtemp = groups[itemp].tmax;
  70.         j = i;
  71.         while (j > 0 && groups[igroup[j-1]].tmax > gtemp)
  72.             {
  73.             igroup[j] = igroup[j-1];
  74.             --j;
  75.             }
  76.         igroup[j] = itemp;
  77.         }
  78.     for (j = 0; j <= itmax; ++j)
  79.         {
  80.         i = igroup[j];
  81.         printf("\nGroup %d: ", j+1);
  82.         printf(" avg=%s", fround(GI.total/GI.n, 4, 4));
  83.         printf(" tmin=%s", fround(GI.tmin, 3, 3));
  84.         printf(" tmax=%s\n", fround(GI.tmax, 3, 3));
  85.         for (ns = 0; ns < GI.n; ++ns)
  86.             printf("\t%s\n", s[GI.members[ns]]);
  87.         }
  88.     }
  89.