/*
  Here is the source code for Efficient C Programming: A Practical Approach,
  by Mark Allen Weiss.  The materials here are copyrighted. 
  (Slight edits by Y. Kohayakawa)
*/

#include <stdio.h>

/* Read Items, Compute Their Average */

int main()
{
    FILE *Fp;                    /* File Pointer */
    char FileName[ 256 ];        /* File Name */
    int ItemsRead = 0;
    double ThisItem, Sum = 0.0;

    printf( "Enter input file: " );
    if( scanf( "%s", FileName ) != 1 ||
            ( Fp = fopen( FileName, "r" ) ) == NULL )
    {
        fprintf( stderr, "Cannot open input file\n" );
        return 1;
    }

    while( fscanf( Fp, "%lf", &ThisItem ) == 1 )
    {
        ItemsRead++;
        Sum += ThisItem;
    }

    printf( "The average of %d items was %f\n",
            ItemsRead, Sum / ItemsRead );

    return 0;
}
