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...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
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...
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT