logo CodeStepByStep logo

average_of_line

Language/Type: PHP basics streams file input
Author: Melissa Hovik (on 2018/11/22)

Write a function named average_of_line that accepts a file name as a parameter and reads that file, assumed to contain a single line of real numbers, returning the average (mean) of the numbers in that line. The parameter, $filename, gives the name of a file that contains the single line of real numbers which are separated by any number of whitespace characters. 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 average_of_line("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.

Function: Write a PHP function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.