rewrote average as a for loop