#C13341. CSV Column Average Calculator
CSV Column Average Calculator
CSV Column Average Calculator
You are given a CSV file content as input via standard input. The first line of the input contains the column name for which an average is to be computed. The second line contains an integer m representing the number of subsequent lines, which together form the CSV file. The CSV file always includes a header row, followed by m lines of data. The task is to calculate and output the average of the numeric values in the specified column. If the column does not exist or if none of the rows contain valid numeric data, output 0.0
.
More formally, let \(X\) be the set of values in the specified column that can be converted to a number. You are to compute and output the average defined as
\[
\text{average} = \frac{\sum_{x \in X} x}{|X|}
\]
if \(|X| > 0\); otherwise, output 0.0
.
The solution should handle erroneous data gracefully by skipping non-numeric values and must use standard input (stdin) for input and standard output (stdout) for output.
inputFormat
The input is given via standard input in the following format:
<target_column> <m> <CSV header row> <data row 1> <data row 2> ... <data row m-1>
Here:
<target_column>
is a string representing the column name whose average needs to be calculated.<m>
is an integer representing the total number of CSV lines that follow (including the header row).- The subsequent lines represent the CSV file content. The first of these lines is the header row.
outputFormat
Output a single line to standard output containing the average as a floating-point number. If no valid numeric entry is found in the specified column or if the column is missing, output 0.0
.
value
6
value
1
2
3
4
5
3.0
</p>