Write a program that computes and prints the average of the numbers in a text file. You should make use of two higher-order functions to simplify the design.




An example of the program input and output is shown below:




Enter the input file name: numbers.txt




The average is 69.83333333333333



______________________________________




Filename: numbers.txt




45 66 88



100 22 98

Respuesta :

CPED

Answer:

Following is the program code as required.

Comments are given inside the code.

Explanation:

(=> Defining the main function)

def main():

(=> Getting input from user)

file = input("What is name of file?")

(=>Storing text from file in variable data )

data = open(file, "r")

(=> read the file and then split it)

info = data.read().split()

(=> Generating array named numbers)

numbers = []

for line in info:

(=>numbers will be appended from in format to list format)

numbers.append(int(line))

data.close()

(=> Average will be found by dividing sum of numbers to length of array)

avg = float(sum(numbers))/len(numbers)

(=> Printing calculation of average)

print("Calculated average is",avg)

main()

i hope it will help you!

The program is an illustration of file manipulations

File manipulations are used to read from and write into a file

The program in Python where comments are used to explain each line is as follows:

#This gets input for the filename

file = input("Filename: ")

#This initializes the sum and count to 0

sum = 0; count = 0

#This opens the file

fopen = open(file, "r")

#This splits the contents of the file

content = fopen.read().split()

#This iterates through the content of the file

for line in content:

   #This calculates the sum of the numbers

   sum +=int(line)

   #This keeps count of the numbers

   count+=1

   #This closes the file

fopen.close()

#This calculates the average

average = float(sum)/count

#This prints the calculated average

print("Average:",average)

Read more about similar programs at:

https://brainly.com/question/20595337