using C++ Learning objective: practice more on using IO Streams, arrays, and lea
ID: 3857351 • Letter: U
Question
using C++
Learning objective: practice more on using IO 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:\temp\sample1.csv c:\temp\sample1_stats.csv
2, 2, 2, 2
4, 4, 0, 4
2, 3, 3, 3
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 .csv file he/she inputs to the program • The user can write the output .csv file to any location; your program should handling 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 functions 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
• [New] To get user’s input of filenames, you may use one of the following two approaches:
1. Specify as command line argument
2. Query user via std::cin
2, 2, 2, 2
4, 4, 0, 4
2, 3, 3, 3
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 sample1.csv sample1_stats.csv
stats.exe 1 c:\temp\sample1.csv c:\temp\sample1_stats.csv
Also Allow your program to take 2 or more input .csv files, merge the data points of each row, and then calculate the statistics of the merged data • Assumptions: – 2 data files have same number of rows… (what if they don’t? how may you address these problem cases? Hint: use try-catch) • Example usage:
sample1.csv
2, 2, 2, 2
4, 4, 0, 4
2, 3, 3, 3
sample2.csv
2, 2
4, 4
0, 0
integraged_stats.csv
Row, Mean, STD
1, 2, 2,
2, 3.30, 1.63
3, 11, 1.47
Explanation / 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.