/* twopass5.cpp illustrates making two passes through a file:
 * one pass to find the average of the number in the file, and
 * a second pass to find the difference between each number and
 * the average.
 *
 * Input:  a series of values from a file named "Data5"
 * Output: each value and its difference from the average value
 *****************************************************************/

#include <iostream.h>     // cout, <<
#include <fstream.h>      // instream, is_open(), eof(), clear(), seekg(),  >> 
#include <assert.h>       // assert

int main()
{
   ifstream inStream("Data5");
   assert(inStream.is_open());

   double newValue,
          sum = 0.0,
          count = 0.0,
          average = 0.0;

   for (;;)
   {
      inStream >> newValue;
      if (inStream.eof()) break;
      sum += newValue;
      count++;
   }

   if (count > 0) average = sum / count;

   inStream.clear();                // clear eof bit
   inStream.seekg(0, ios::beg);     // reset read position

   for (;;)
   {
      inStream >> newValue;
      if (inStream.eof()) break;
      cout << newValue << ": " << newValue - average << endl;
   }

   inStream.close();

   return 0;
}


