Check that you are initialising sum to zero and that number_of_elements has the correct value. Also check that you are supposed to subtract one from it. (there are different ways of defining it). And check that average has the value you expect.