{--------------------------------------------------------------}
{                           Average                            }
{                                                              }
{             Binary file I/O demonstration program            }
{                                                              }
{                             by Jeff Duntemann                }
{                             Turbo Pascal V3.0                }
{                             Last update 5/1/86               }
{                                                              }
{    From the book, COMPLETE TURBO PASCAL, by Jeff Duntemann   }
{    Scott, Foresman & Co. (c) 1986,1987  ISBN 0-673-18600-8   }
{--------------------------------------------------------------}

PROGRAM Average;

VAR 
  IntFile       : FILE OF Integer;
  I,J,Count     : Integer;
  Average,Total : Real;

BEGIN
  Assign(IntFile,'INTEGERS.BIN');
  {$I-} Reset(IntFile) {$I+}
  I := IOResult;
  IF I <> 0 THEN
    BEGIN
      Writeln('>>File INTEGERS.BIN is missing or damaged.');
      Writeln('  Please investigate and run the program again.')
    END
  ELSE
    BEGIN
      Count := 0; Total := 0.0;
      WHILE NOT EOF(IntFile) DO
        BEGIN
          Read(IntFile,J);
          Count := Count + 1;
          Total := Total + J;
        END;
      Close(IntFile);
      AVERAGE := Total / Count;  
      Writeln;
      Writeln('>>There are ',Count,' integers in INTEGERS.BIN.');
      Writeln('  Their average value is ',Average:10:6,'.');
    END
END.
