You also don't need the mx--. It was this that was making it "work" when the entered values were consecutive. Consequently you also don't need mx2 as mx doesn't change over the output loop.