total = 0 count = 0; avg = 0; for each item { total = total + margin count++ } if (count > 0) { avg = total / count }