#C14611. Moving Average Calculation
Moving Average Calculation
Moving Average Calculation
You are given a list of integers and a sliding window size. Your task is to compute the moving average for each contiguous subarray of the list having length equal to the window size. The moving average is defined as:
$$\text{avg}_i = \frac{\sum_{j=i}^{i+\text{window\_size}-1}\text{nums}[j]}{\text{window\_size}}$$
Each computed average must be rounded to two decimal places. If the list length is less than the window size, output an empty result.
Note: The input will be provided via standard input (stdin) and your program should print the output to standard output (stdout).
inputFormat
The input consists of three lines:
- The first line contains an integer
n
representing the number of elements in the list. - The second line contains
n
space-separated integers. - The third line contains an integer
window_size
which is the size of the sliding window.
outputFormat
Output a single line containing the moving averages for each valid window. Each average must be printed as a floating-point number rounded to two decimal places and separated by a space. If no window is valid, output an empty line.
## sample5
1 2 3 4 5
3
2.00 3.00 4.00