tang/CSC1125/csa125 AS.pdi Learning objective: practice more on using 10 Streams
ID: 3857324 • Letter: T
Question
tang/CSC1125/csa125 AS.pdi Learning objective: practice more on using 10 Streams, arrays, and learn to implement algorithms. Program objective: Write a program that allows the user to calculate statistics in a given input file. Example usage stats.exe 1 c:lltempIsamplel.csv c: emplsamplel stats.csv sample1.csv 2, 2, 2, 2 4, 4, 0, 4 2, 3, 3,3 sample1_stats.csv Row, Sum, Mean, STD, Median, Mode, Min, Max 1,8, 2,0,2, 2,2,2 2, 12, 3, 2,4,4,0,4 3, 11, 2.75, 0.50, 3,3, 2,3 Requirements: .The user can provide the path to any esv file he/she inputs to the program. (See notes on CSV at the bottom . The user can write the output.csv file to any location; your program should handing inaccessible paths gracefully Use floating point with 2 significant digits. .You must use loop(s) to calculate the statistics. You must not use any built-in funetions to calculate any of these statistics The argument "1" determines the statistics to write to file: -1: all statistics (as shown in example above) 2: calculate and print the mean and STD only .INew] To get user's input of filenames, you may use one of the following two approaches: Specify as command line argument (example shown in above) Query user via std:cin 1. 2. Assignment 5 (cont'd to search F12 FB F5 F6 7 8Explanation / Answer
inputFile = open("test.txt", "r") outputFile = open("test2.txt", "a") outputFile.write("myTotal, myMean, myStd, myMedian, myMode, myMinimum, myMaximum ") count = 0 for line in inputFile: count += 1 infileline = line.rstrip().split(",") infileline.sort() while line != "": myTotal = float(infileline[0]) + float(infileline[1]) + float(infileline[2]) + float(infileline[3]) myMean = myTotal / 4 myStd = ((float(infileline[0])**2 + float(infileline[1])**2 + float(infileline[2])**2 + float(infileline[3])**2) / 4) - myMean**2 myMedian = (float(infileline[1]) + float(infileline[2])) / 2 myMode = 2 myMinimum = float(infileline[0]) myMaximum = float(infileline[3]) outputFile.write(str(count)+',') outputFile.write(','.join("%1.0f"%i for i in (myTotal, myMean, myStd, myMedian, myMode, myMinimum, myMaximum))) outputFile.write(' ') inputFile.close() outputFile.close()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.