xaahm18
Look at what you are modifying:
input.push_back(...
Also, I think you need to spend more time thinking about the way
cumulative sum works.
Cumulative sum is also known as
partial sum or
running total. Let’s look at an example.
Given a list of numbers:
1 7 -3 4
We can compute the average by adding them all together and dividing by the number of items:
(1 + 6 + -3 + 4) / 4
(7 + -3 + 4) / 4
(4 + 4) / 4
(8) / 4
2
The reduction we do when summing all the elements (before dividing) is the basic idea of the partial sum. At each step we keep a running total/partial sum/cumulative sum, which we see: first 1, then 7, then 4, then 8.
With the cumulative sum algorithm, we don’t care for only the
final sum, but we want all the partial sums as well. So our list becomes:
┌───┬───┬───┬───┐
│ 5 │ 7 │-2 │ 6 │ (source data, AKA input)
└───┴───┴───┴───┘
0
┌───┬───┬───┬───┐
│ │ │ │ │ (cumulative sums, AKA output)
└───┴───┴───┴───┘ |
┌───┬───┬───┬───┐
│ 5 │ 7 │-2 │ 6 │
└───┴───┴───┴───┘
0 + 5
┌───┬───┬───┬───┐
│ 5 │ │ │ │
└───┴───┴───┴───┘ |
┌───┬───┬───┬───┐
│ 5 │ 7 │-2 │ 6 │
└───┴───┴───┴───┘
5 + 7
┌───┬───┬───┬───┐
│ 5 │12 │ │ │
└───┴───┴───┴───┘ |
┌───┬───┬───┬───┐
│ 5 │ 7 │-2 │ 6 │
└───┴───┴───┴───┘
12 +-2
┌───┬───┬───┬───┐
│ 5 │12 │10 │ │
└───┴───┴───┴───┘ |
┌───┬───┬───┬───┐
│ 5 │ 7 │-2 │ 6 │
└───┴───┴───┴───┘
10 + 6
┌───┬───┬───┬───┐
│ 5 │12 │10 │16 │
└───┴───┴───┴───┘ |
Your task is to take an input array/vector/something and produce an output array/whatever full of the partial sums.
My suggestion:
• write a loop that computes the sum (as if you were computing an average)
• add output into the loop
Hope this helps.