/*---------------------------------------------------*/
/*  Program chapter3_6                               */
/*                                                   */
/*  This program generates a summary report from     */
/*  a data file that has a trailer record with       */
/*  negative values.                                 */

#include <stdio.h>
#define FILENAME "sensor2.dat"

int main(void)
{
   /*  Declare and initialize variables.  */
   int num_data_pts=0, k;
   double time, motion, sum=0, max, min;
   FILE *sensor2;

   /*  Open file and read the first data points.  */
   sensor2 = fopen(FILENAME,"r");
   fscanf(sensor2,"%lf %lf",&time,&motion);

   /*  Initialize variables using first data points.  */
   max = min = motion;

   /*  Update summary data until trailier record read.  */
   do
   {
      sum += motion;
      if (motion > max)
	 max = motion;
      if (motion < min)
	 min = motion;
      num_data_pts++;
      fscanf(sensor2,"%lf %lf",&time,&motion);
   }  while (time >= 0);

   /*  Print summary information.  */
   printf("Number of sensor readings: %i \n",
	  num_data_pts);
   printf("Average reading:           %.2f \n",
	  sum/num_data_pts);
   printf("Maximum reading:           %.2f \n",max);
   printf("Minimum reading:           %.2f \n",min);

   /*  Close file and exit program.  */
   fclose(sensor2);
   return 0;
}
/*---------------------------------------------------*/

