//==================================================== file = smooth1.c =====
//=  Program to implement exponential smooth for a series X of size N       =
//=   - Implements f_t+1 = f_t + alpha*(y_t - f_t) for current sample y_t   =
//===========================================================================
//=  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) Output is to stdout                                               =
//=    3) Must manually set #define ALPHA                                   =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=    & Sample series of data which can be integers or reals.              =
//=      There are 11 values in this file. &                                =
//=    50                                                                   =
//=    42                                                                   =
//=    48                                                                   =
//=    61                                                                   =
//=    60                                                                   =
//=    53                                                                   =
//=    39                                                                   =
//=    54                                                                   =
//=    42                                                                   =
//=    59                                                                   =
//=    53                                                                   =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat"):                                    =
//=                                                                         =
//=    ----------------------------------------------- smooth1.c -----      =
//=      Input and smoothed values for alpha = 0.500000 (N = 11)            =
//=        50.000000  50.000000                                             =
//=        42.000000  50.000000                                             =
//=        48.000000  46.000000                                             =
//=        61.000000  47.000000                                             =
//=        60.000000  54.000000                                             =
//=        53.000000  57.000000                                             =
//=        39.000000  55.000000                                             =
//=        54.000000  47.000000                                             =
//=        42.000000  50.500000                                             =
//=        59.000000  46.250000                                             =
//=        53.000000  52.625000                                             =
//=        Forecast next value is 52.812500                                 =
//=    ---------------------------------------------------------------      =
//=-------------------------------------------------------------------------=
//=  Build: gcc smooth1.c, bcc32 smooth1.c, cl smooth1.c                    =
//=-------------------------------------------------------------------------=
//=  Execute: smooth1 < in.dat                                              =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (05/23/00) - 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 MAX_SIZE 1000000L          // Maximum size of time series data array
#define ALPHA        0.50          // Alpha value (between 0 and 1)

//----- Globals -------------------------------------------------------------
double     X[MAX_SIZE];            // Time series read from "in.dat"
long int   N;                      // Number of values in "in.dat"

//----- Function prototypes -------------------------------------------------
void   load_X_array(void);         // Load X array

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  double   f_current;                // Current smoothed value
  double   f_next;                   // Next smoothed value
  long int i;                        // Loop counter

  // Load the series X
  printf("----------------------------------------------- smooth1.c -----\n");
  load_X_array();

  // Ouput run information
  printf("  Input and smoothed values for alpha = %f (N = %d) \n", ALPHA, N);

  // Initialize f_current to X[0] and then do smoothing
  f_current = X[0];
  for (i=0; i<N; i++)
  {
    f_next = f_current + ALPHA * (X[i] - f_current);
    printf("    %f  %f \n", X[i], f_current);
    f_current = f_next;
  }

  // Output forecast value (f_next) for end of series
  printf("    Forecast next value is %f \n", f_next);
  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;
}

