//===================================================== file = count2.c =====
//=  Program to tabulate the time for a given byte count                    =
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from input file "in.dat" to stdin (see example below)       =
//=        * Input is two columns of reals with no commas                   =
//=        * No comments are allowed in the input file                      =
//=    3) The byte count value COUNT_VALUE is in the #define section        =
//=    2) It is assumed that the "bytes" are always 1 so that an exact      =
//=       count can be achieved for each time interval                      =
//=    3) Output is to stdout                                               =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=  0.01   1                                                               =
//=  0.05   1                                                               =
//=  0.05   1                                                               =
//=  0.03   1                                                               =
//=  0.02   1                                                               =
//=  0.04   1                                                               =
//=  0.06   1                                                               =
//=  0.03   1                                                               =
//=  0.12   1                                                               =
//=  0.04   1                                                               =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat" and COUNT_VALUE = 3)                 =
//=                                                                         =
//=  & ------------------------------------------- count2.c ----- &         =
//=  0.110000                                                               =
//=  0.090000                                                               =
//=  0.210000                                                               =
//=  & ---------------------------------------------------------- &         =
//=-------------------------------------------------------------------------=
//=  Build: bcc32 count2.c                                                  =
//=-------------------------------------------------------------------------=
//=  Execute: count2 < in.dat                                               =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (06/07/02) - Genesis                                      =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf() and feof()
#include <stdlib.h>                // Needed for exit() and atof()
#include <string.h>                // Needed for strcmp()

//----- Defines -------------------------------------------------------------
#define COUNT_VALUE       3        // Count value for a "block"

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  double    time_stamp;            // Delta timestamp value of a tuple
  double    num_bytes;             // Number of bytes of a tuple
  double    sum_time;              // Time sum for this interval
  double    sum_bytes;             // Bytes sum for this interval
  char      instring1[1024];       // Input string #1
  char      instring2[1024];       // Input string #2

  // Output a banner
  printf("& ------------------------------------------- count2.c ----- & \n");

  // Compute and output interval times
  sum_bytes = sum_time = 0.0;
  while(!feof(stdin))
  {
    // Get time_stamp and num_butes
    scanf("%s  %s \n", instring1, instring2);
    time_stamp = atof(instring1);
    num_bytes = atof(instring2);

    // Keep running sums
    sum_time = sum_time + time_stamp;
    sum_bytes = sum_bytes + num_bytes;

    // If sum_bytes is COUNT_VALUE output sum_time and reset all sums
    if (sum_bytes >= COUNT_VALUE)
    {
      printf("%f \n", sum_time);
      sum_time = sum_bytes = 0.0;
    }
  }

  // Output closing message
  printf("& ---------------------------------------------------------- & \n");
}

