In: Computer Science
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display the resulting output for Items A-G.
NOTE: Since the programming language is not specified I am doing in python, I am doing all the A-G questions in one program itself, If data is "," specified or any other delimeter then read data at once and use split function to get data and the operations remain same just in the place of appending we use split function
EXPLANATION: Explanation is done in the code itself. Place the file in the same directory where the source file is saved. Other wise keep the absolute path
SOLUTION FOR A-G :
##open the file in read mode
data=open("Lab6A_Data.txt","r")
dataList=[]
##read the data and append it to the list so that it will be
easy to calculate
for i in data.readlines():
##if there is \n remove that
if(i.find("\n")):
i.replace("\n","")
##convert it to int while appending so that the operations are done
easily
dataList.append(int(i))
##SOLUTION A:
##len is used to get length of the list that means that means
count
print("Number of values in file is :",len(dataList))
##SOLUTION B:
##sum is used to calculate sum of items
print("Sum of all the values : ",sum(dataList))
##SOLUTION C:
##average is calculated based as the sum of items by number of
items
print("Average of all the values in the file: "
,round(sum(dataList)/len(dataList)),2)
##SOLUTION D:
##min will return the min value in the list
print("Minimum value : " ,min(dataList))
##SOLUTION E:
##max will return the max value in the list
print("Minimum value : " ,max(dataList))
##SOLUTION F:
##max will return the max value in the list
print("Range is from ", min(dataList) ," to ",max(dataList))
##SOLUTION G:
##count function will return number of items in the list
print("The number of time 327 occurs is :
",dataList.count(327))
CODE IMAGE:
OUTPUT:
SOURCE FILE WHICH I HAVE TAKEN: