Modifed to use generalised array for weighted mean