Reputation: 5
I have a function two sum up, in a different way, values of a Matrix.
The different way; with transposing the matrix.
int matrixElementsSum(vector<vector<int>> matrix) {
int sum = 0;
vector<vector<int>> tmatrix;
for(int i = 0; i < matrix[i].size(); i++)
{
vector<int> row;
for(int j = 0; j < matrix.size(); j++)
{
row.push_back(matrix[j][i]);
}
cout << "Controlling the size" ;
cout << " " << matrix[i].size() << endl;
tmatrix.push_back(row);
}
for(int i = 0; i < tmatrix.size(); i++)
{
for(int j = 0; j < tmatrix[i].size(); j++)
{
if(tmatrix[i][j] == 0) break;
sum += tmatrix[i][j];
}
}
return sum;
}
input:
[[5, 10, 15] ,[20, 25, 30]]
output:
Controlling the size 3
Controlling the size 3
Controlling the size 8
105
And my question is: when the size of my vector is changed from 3 to 8? And also how?
Upvotes: 0
Views: 38
Reputation: 1988
Shouldn't it be
for(int i = 0; i < matrix.size(); i++)
and
for(int j = 0; j < matrix[i].size(); j++)
?
Upvotes: 1