partial_sum of 2d vector

Hi everyone, I'm new to c++ programming. I need to compute partial sum for the 2d vector. I had try to coded it but errors occurred. Hope anyone can help me.
Thank you.

Original data:
0 3 6 9 7 5 0
0 4 12 8 10 3 0
0 12 11 6 4 7 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream> 
#include <vector> 
#include <fstream>
#include <numeric>  // for partial_sum

using namespace std;

const int row = 3;	
const int col = 7;	

vector<vector<int> > data(row, vector <int>(col, 0));

int i, j;

int main()
{
	// Input file
	ifstream inpData("myData.txt");

	// Assign data into array 
	cout << "Original data: "; 
		cout << endl;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			inpData >> data[i][j];
			cout << data[i][j] << '	';
		}
		cout << endl;
	}
	cout << endl;

	// Close input files
	inpData.close();
	

	cout << "Partial sum of 2d vector:"; 
		cout << endl;

	// Partial sum of 2d vector
	vector<vector<long double> > sums;
	
	for(i = 0; i < row; i++)
	{
		partial_sum(data.begin(), data.end(), sums.begin());
	}

	// Display the solution
	for(i = 0; i < row; i++)
	{
		for(int j = 0; j < col; j++)
		{
			cout << sums[i][j] << '	';
		}
		cout << endl;
	}

	
	system("pause");
}
Here's how you would do it with arrays and with for-loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream> 
using namespace std;

int main()
{
	constexpr int rows = 3;
	constexpr int cols = 2;
	int arr[rows][cols] = { {2,2}, {3,3}, {4,4} };
	int row_sum[rows] = { 0 }, col_sum[cols] = { 0 };

	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			row_sum[i] += arr[i][j];
			col_sum[j] += arr[i][j];
		}
	}

	for (int i = 0; i < rows; i++) {
		std::cout << row_sum[i] << ' ';
	}
	std::cout << '\n';
	for (int j = 0; j < cols; j++) {
		std::cout << col_sum[j] << ' ';
	}

	std::cin.get();
}


You can do it with partial_sum, but you gotta use indexes. i.e data[i].begin() not data.begin().
What does it mean to partially sum a 2D vector?
Equivalently, what does it mean to sum two vectors?
Hi Grime, do mean like this, but errors still occured.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream> 
#include <vector> 
#include <fstream>
#include <numeric>  // for partial_sum

using namespace std;

const int row = 3;	
const int col = 7;	

vector<vector<int> > data(row, vector <int>(col, 0));

int i, j;

int main()
{
	// Input file
	ifstream inpData("myData.txt");

	// Assign data into array 
	cout << "Original data: "; 
		cout << endl;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			inpData >> data[i][j];
			cout << data[i][j] << '	';
		}
		cout << endl;
	}
	cout << endl;

	// Close input files
	inpData.close();
	

	cout << "Partial sum of 2d vector:"; 
		cout << endl;

	// Partial sum of 2d vector
	vector<vector<int> > sums;
	
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < row; j++)
		{
			partial_sum(data[i].begin(), data[i].end(), sums.begin());
		}
	}

	// Display the solution
	for(i = 0; i < row; i++)
	{
		for(int j = 0; j < col; j++)
		{
			cout << sums[i][j] << '	';
		}
		cout << endl;
	}
	system("pause");
}
Hi mbozzi and Grime,

Oh I think i used the wrong statement,
partial sum for the 2d vector
.

This is the original data:

0 3 6 9 7 5 0
0 4 12 8 10 3 0
0 12 11 6 4 7 0

After do partial_sum, I want the solution be something like this:

0 3 9 18 25 30 30
0 4 16 24 34 37 37
0 12 23 29 33 40 40

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <numeric>

std::vector<int> partial_sums( const std::vector<int>& vec )
{
    std::vector<int> result( vec.size() ) ;
    std::partial_sum( vec.begin(), vec.end(), result.begin() ) ;
    return result ;
}

int main()
{
    const std::vector< std::vector<int> > data
    {
        { 0, 3, 6, 9, 7, 5, 0 },
        { 0, 4, 12, 8, 10, 3, 0 },
        { 0, 12, 11, 6, 4, 7, 0 }
    };

    std::vector< std::vector<int> > sums ;
    for( const auto& row : data ) sums.push_back( partial_sums(row) ) ;

    for( const auto& row : sums )
    {
        for( int s : row ) std::cout << s << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/3d89c4f2d1a88c6a
There needs to be space in the vector of partial sums for the result.
It's also possible to use std::back_insert_iterator to make space only when required, but there is little point.

1
2
3
4
5
6
7
// reserve space for the partial sums
vector<vector<int> > sums(row, std::vector<int>(col));

for(i = 0; i < row; i++)
{
	partial_sum(data[i].begin(), data[i].end(), sums[i].begin());
}


The #include <cstdlib> required to portably use std::system is missing.
The name data has been adopted by the C++17 standard library, so your program won't port to the newest version of the language thanks to the ham-fisted using namespace std.

It looks like you are using C++98.

Last edited on
Oh, that why i can't get the result. I didn't reserve space for the result.

Yes i still using c++98.

Thanks a lot for your help,mbozzi. Also toJLBorges and Grime, thank you.
Really appreciated.
Last edited on
Topic archived. No new replies allowed.