我试图弄清楚如何编写一个循环来计算数组中元素的总和,直到当前索引。这是用于计算前缀平均值的解决方案的一部分。到目前为止,我已经包含了我的方法(包括我正在尝试编写的伪代码)。我将不胜感激提供的任何帮助!
// getAverages method populates the b array with the prefix averages
public void getAverages()
{
// A prefix average is calculated by averaging all the numbers up to your current index.
// b[i] is the average of a[0]…a[i], for 0 <= i <= n
for (int i = 0; i < a.length; i++)
{
b[i] = the sum of all a indexes up to i divided by (i + 1)
}
}