PrefixAverages1(X)
Input: X, a 1-D numerical array of size n
1) Let A = an empty 1-D numerical array of size n
2) For i = 0 to n-1
3) Let s = X[0]
4) For j = 1 to i
5) Let s = s + X[j]
6) End For
7) Let A[i] = s /(i+1)
8) End For
Output: An n-element array A of numbers such that A[i]
is the average of elements X[0],X[1], … ,X[i]
PrefixAverages2(X)
Input: X, a 1-D numerical array of size n
1) Let A = an empty 1-D numerical array of size n
2) Let s = 0
3) For i = 0 to n-1
4) Let s = s + X[i]
5) Let A[i] = s / (i+1)
6) End For
Output: An n-element array A of numbers such that A[i]
is the average of elements X[0],X[1], … ,X[i]
So here is what I know so far:
The first algorithm makes use of a second nested for loop. The second one is more efficient. In the first, S is equal to the first element of the array. In the second, S is equal to 0.
What else am I missing? Any help will really be appreciated, thanks!