216k views
0 votes
Assume that a file containing a series of integers is named numbers.dat and exists on the computer's disk. design a programt hat calculates the average of all the numbers stored in the file

User Han Pengbo
by
7.1k points

2 Answers

6 votes
This is what I got so far, I hope this helped you!
Assume that a file containing a series of integers is named numbers.dat and exists-example-1
User Maggon
by
7.4k points
3 votes

Answer:

Python code:

#main method

def main():

#variable to store sum of the numbers

addition = 0.0

#variable to store count of numbers

n = 0

#opens the file numbers.dat as a random file

with open('numbers.dat') as numbers_file:

#for loop to read data from the file

for line in numbers_file:

#adding the line into the variable

addition+=int(line)

#increment the variable n

n+=1

#displays the average of numbers stored in the file

print("average of all the numbers stored in 'numbers.dat' is:",format((addition/n),'.2f'))

main()

Step-by-step explanation:

To find the average of integers stored in the file “numbers.dat”. A file is created and some integer values are stored in it.

Note: The data stored in the file is shown in the output section.

In the python program, firstly the main method is defined and the variables are initialized as per the requirement (the use of each variable is shown as comments in the program).

The with open() function is used to open the file “numbers.dat”. And the for-loop is used to read the numbers line by line.

The print() method is used to display the average of the numbers.

Output:

Assume that a file containing a series of integers is named numbers.dat and exists-example-1
User Jona Rodrigues
by
8.8k points