Changed line:
r = r + (int) (1.0 / n);
on:
r += (int) (1.0 / n);