//=================================================== file = interval.c =====
//=  Program to compute the interval means of a time series X               =
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from input file "in.dat" to stdin (see example below)       =
//=        * Comments are bounded by "&" characters at the beginning and    =
//=          end of the comment block                                       =
//=    2) The number of intervals for N values is M in the #define section  =
//=    3) If mod(N,M) is not zero, then the last remainder values are not   =
//=       included in the last interval mean                                =
//=    4) Output is to stdout                                               =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=   & Here is a series of 7 values to be intervaled with M = 2  &         =
//=   21                                                                    =
//=   3                                                                     =
//=   55                                                                    =
//=   45                                                                    =
//=   12                                                                    =
//=   5                                                                     =
//=   19                                                                    =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat" and M = 2):                          =
//=                                                                         =
//=   & ------------------------------------------ interval.c ----- &       =
//=   26.333333                                                             =
//=   20.666667                                                             =
//=   &  Output 2 means for an interval size of 3 and 7 values              =
//=     ---------------------------------------------------------- &        =
//=-------------------------------------------------------------------------=
//=  Build: gcc interval.c, bcc32 interval.c, cl interval.c                 =
//=-------------------------------------------------------------------------=
//=  Execute: interval < in.dat                                             =
//=-------------------------------------------------------------------------=
//=  Author: Zane Reynolds                                                  =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~zreynold                         =
//=          Email: zreynold@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: ZR (9/20/00) - Genesis (from block.c)                         =
//===========================================================================

//----- 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 MAX_SIZE 1000000L          // Max size of time series data array
#define M              2L          // Number of intervals

//----- Globals -------------------------------------------------------------
double     X[MAX_SIZE];            // Time series read from "in.dat"
long int   N;                      // Number of values in X[]

//----- Prototypes ----------------------------------------------------------
void   load_X_array(void);         // Load X array

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  long int  int_size;              // Number of values in each interval
  double    int_mean;              // Compute block mean
  double    sum;                   // Temporary sum variable
  long int  i, j;                  // Loop counters

  // Load the series X
  printf("& ------------------------------------------ interval.c ----- & \n");
  load_X_array();

  // Compute and output M interval means
  int_size = (long int) N / M;
  for (i=0; i<M; i++)
  {
    sum = 0;
    for (j = (i*int_size); j<=(((i+1)*int_size)-1); j++)
      sum = sum + X[j];

    int_mean = sum / int_size;
    printf("%f \n", int_mean);
  }

  // Output closing message
  printf("&  Output %ld means for an interval size of %ld and %ld values \n",
    M, int_size, N);
  printf("  ---------------------------------------------------------- & \n");
}

//===========================================================================
//=  Function to load X array from stdin and determine N                    =
//===========================================================================
void load_X_array(void)
{
  char      temp_string[1024];     // Temporary string variable

  // Read all values into X
  N = 0;
  while(1)
  {
    scanf("%s", temp_string);
    if (feof(stdin)) goto end;

    // This handles a comment bounded by "&" symbols
    while (strcmp(temp_string, "&") == 0)
    {
      do
      {
        scanf("%s", temp_string);
        if (feof(stdin)) goto end;
      } while (strcmp(temp_string, "&") != 0);
      scanf("%s", temp_string);
      if (feof(stdin)) goto end;
    }

    // Enter value in array and increment array index
    X[N] = atof(temp_string);
    N++;

    // Check if MAX_SIZE data values exceeded
    if (N >= MAX_SIZE)
    {
      printf("*** ERROR - greater than %ld data values \n", MAX_SIZE);
      exit(1);
    }
  }

  // End-of-file escape
  end:

  return;
}

