Well one issue you might have is that x,y,z are int and on a uno int are on 16 bits, signed - so max is +32767
When you do your average 40 times (in my opinion it’s useless and reading 2 different analog input very quickly can actually lead to wrong reading) if the value is close to the max then you overflow your int