Write math average function