//================================================= file = predictBit.c =====
//=  Scafold for a bit prediction program                                   =
//=   - For assignment #6 of Simulation (CIS 4930) summer 2011              =
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from file "bits.txt" (must exist in execution directory)    =
//=    2) Output is to stdout                                               =
//=-------------------------------------------------------------------------=
//= Example execution:                                                      =
//=  << to be filled-in by student >>                                       =
//=                                                                         =
//=-------------------------------------------------------------------------=
//=  Build: standard C build                                                =
//=-------------------------------------------------------------------------=
//=  Execute: summary1                                                      =
//=-------------------------------------------------------------------------=
//=  Author: Ken Christensen                                                =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (07/02/11) - Genesis                                      =
//===========================================================================
//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for fscanf() and printf()
#include <stdlib.h>                // Needed for exit() and atoi()

//----- Defines -------------------------------------------------------------
#define NUM_BITS  1000000          // Number of bits in bits.txt

//----- Prototypes ----------------------------------------------------------
int predict(int currentBit);       // Function to predict next bit

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main()
{
  FILE *fp;                        // File pointer
  char inString[256];              // Input string
  int  *bits;                      // Array of bits from bits.txt
  int  currentBit;                 // Value of "this" (current) bit
  int  predictBit;                 // Value of predicted next bit
  int  errorSum;                   // Sum of errors (mispredictions)
  int  i;                          // Loop counter

  // Allocate memory for bits array
  bits = (int *) malloc(sizeof(int) * NUM_BITS);
  if (bits == NULL)
  {
    printf("*** Error - could not malloc for *bits \n");
    exit(1);
  }

  // Open the input file (bits.txt)
  fp = fopen("bits.txt", "r");
  if (fp == NULL)
  {
    printf("*** Error - could not find file 'bits.txt' \n");
    exit(1);
  }

  // Read NUM_BITS into bits[] from input file
  for (i=0; i<NUM_BITS; i++)
  {
    fscanf(fp, "%s", inString);
    bits[i] = atoi(inString);
  }

  // Main loop to determine prediction error
  printf("Running... \n");
  errorSum = 0;
  predictBit = 0;
  for (i=0; i<NUM_BITS; i++)
  {
    // Grab next (now current) bit
    currentBit = bits[i];

    // Test against prediction for this bit
    if (currentBit != predictBit)
      errorSum++;

    // Predict the next bit
    predictBit = predict(currentBit);
  }

  // Output prediction error
  printf("Prediction error = %f %% \n", (100.0 * errorSum / NUM_BITS));
}

//---------------------------------------------------------------------------
//- Function to predict next bit                                            -
//---------------------------------------------------------------------------
int predict(int currentBit)
{
  int predictNextBit;              // Predicted (next) bit

  // << to be filled-in by student >>

  return(predictNextBit);
}

