Question

In: Computer Science

Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...

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.

Solutions

Expert Solution

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:


Related Solutions

Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
4. Write a program that reads all numbers from a file and determines the highest and...
4. Write a program that reads all numbers from a file and determines the highest and lowest numbers. You must NOT use arrays to solve this problem! Write functions where appropriate. Programming language should be C
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a program that reads from the external file input.txt, capitalizes all words that begin with...
Write a program that reads from the external file input.txt, capitalizes all words that begin with the letter "a," and then writes them to an external file output.txt (Note: Do not forget to copy the blanks. You may wish to use infile.get and outfile.put in your program.) Output: After the program generates output.txt, the code should display the contents of the file on the screen to verification.
Write a program that prompts the user to enter a file name, then opens the file...
Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT