Cumulative Sum

Mar 2, 2019 at 12:59pm
I made this program but i cant make this work...
#include <iostream>
#include <vector>

using namespace std;

void cumulativeSum(vector<float> input, vector<float> result){
vector<float> a;
for(int i; i < input.size();i++){
input.push_back(input[0+i]+input[1+i]);
result = input;
}

}


int main() {

vector<float> v2{23,1243.3,23.23,34.2323,-2343.33,3434.3,12112.1};
vector<float> res2;
cumulativeSum(v2, res2);
for (auto &r : res2)
cout << r << ", ";
cout << endl;

return 0;
}
Mar 2, 2019 at 1:18pm
1
2
3
4
5
6
void cumulativeSum(const vector<float>& input, vector<float>& result) {
	result.push_back(input[0]);
	for (int i = 1; i < input.size(); i++) {
		result.push_back(result[i - 1] + input[i]);
	}
}
Mar 2, 2019 at 7:33pm
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main()
{
   vector<double> V{ 23, 1243.3, 23.23, 34.2323, -2343.33, 3434.3, 12112.1 };
   vector<double> S( V.size() );
   partial_sum( V.begin(), V.end(), S.begin() );
   for ( auto s : S ) cout << s << " ";
}
Mar 2, 2019 at 10:27pm
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.
Last edited on Mar 2, 2019 at 10:27pm
Mar 4, 2019 at 3:23pm
tnx
Mar 4, 2019 at 3:24pm
i did numerous ways but i don't think to make it work
Mar 4, 2019 at 5:55pm
Can you write a program to simply sum the numbers in a vector?

The variables you need are:
  • the vector
  • the index into the vector (as in myvec[n])
  • the current sum

Do this and you will be 84% done.
Topic archived. No new replies allowed.