in reply to counting inside the loop

The usual way of computing a maximum is to keep a current maximum (for instance initialized to zero), and, whenever you encounter a value which is larger than the current maximum, adjust the current maximum.

-- 
Ronald Fischer <ynnor@mm.st>