logo CodeStepByStep logo

averageValueInFile

Language/Type: C++ basics streams file input
Related Links:

Write a function named averageValueInFile that accepts a file name as a parameter and reads that file, assumed to be full of real numbers, and returns the average (mean) of the numbers in that file. The parameter, filename, gives the name of a file that contains a list of real numbers. There could be anywhere from zero to many values per line separated by any number of blank spaces. You may assume that the file exists and follows the proper format. For example, if a file named input.txt contains the following numbers

 1.5
  2.75  9.0

-3.25  0.0      6.5

Then the call of averageValueInFile("input.txt"); should return 2.75.

You may assume that the input file exists and is in the proper format. If the file does not contain any input values, return 0.0.

Constraints: Your solution should read the file only once, not make multiple passes over the file data.

Type your C++ solution code here:


This is a function exercise. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.