[Solved] How to compute the average for every n number in a list [closed]


public List<double> Average(List<double> number, int nElement)
    {
        var currentElement = 0;
        var currentSum = 0.0;

        var newList = new List<double>();

        foreach (var item in number)
        {
            currentSum += item;
            currentElement++;

            if(currentElement == nElement)
            {
                newList.Add(currentSum / nElement);
                currentElement = 0;
                currentSum = 0.0;
            }
        }
        // Maybe the array element count is not the same to the asked, so average the last sum. You can remove this condition if you want
        if(currentElement > 0)
        {
            newList.Add(currentSum / currentElement);
        }

        return newList;
    }

solved How to compute the average for every n number in a list [closed]