This has something to do with the way that the numbers are being subtracted. Change setprecision(1) to setprecision(15) and you can see that it is not subtracting exactly 0.1, but instead a number very close to it.
When 2 is entered, it ends like this:
1 2
20 0.099999999999999
21 -0.000000000000001
This rounds to -0.0
When 5 is entered, it ends like this:
1 2
51 0.000000000000001
52 -0.099999999999999
This rounds to -0.1
Sorry, I do not why it does this or how to fix it, but I am interested to see what the answer is.
Edit: Actually, I found this: http://docs.sun.com/source/806-3568/ncg_goldberg.html, a paper called What Every Computer Scientist Should Know About Floating-Point Arithmetic by David Goldberg, which may shed some light on this situation.