Smoothing a Noisy Line With a Rolling Mean
Halden Cycle Hire's daily return counts jump around so much that nobody trusts a single day. The board therefore quotes a smoothed headline figure: the average over the most recent stretch of days, with the length of that stretch set by whoever is asking. The chart shows the raw counts and the smoothed line together; the board slide needs today's smoothed figure as a number.
Task: Print the average of the last w readings.
Input
The first line holds one integer n, the number of daily readings, where 1 <= n <= 1000. Then come n lines, one reading each, oldest first. The final line holds one integer w, the length of the stretch to average, where 1 <= w <= n.
Output
One line holding the arithmetic mean of the last w readings, rounded to 2 decimal places. Only those w readings count; earlier days sit outside the window and contribute nothing.
Example:
Input:
5
10
20
30
40
50
3
Output:
40.00
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in