I am trying to figure out how to write a loop that will calculate the sum of elements in an array up to the current index. This is for part of a solution for calculating prefix averages.
I have included my method so far (including the pseudo-code I am trying to write).
I would appreciate any help offered!
// 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)
}
}
sumstores all the sum up to the current index. I hope it solves your problem.