My guess would be that sum is meant to be a counter for the number of times gone though the loop so could you try use 1?