I wrote the following program in C++ and it turned up the following errors. It probably seems to be a problem with traversing the 2D vector
I have already tried out writing int j = matrix.at(0).begin()
but that doesn't work as well
I have come across the following errors
invalid operands to binary expression ('vector<int>::iterator' (aka '__wrap_iter<int *>') and 'std::__1::vector<int, std::__1::allocator<int> >::size_type' (aka 'unsigned long'))
no viable conversion from 'std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::iterator' (aka '__wrap_iter<std::__1::vector<int, std::__1::allocator<int> > *>') to 'int'
#include <stdio.h>
#include <vector>
#include <iterator>
using namespace std;
int diagonalSum(vector<vector<int> > matrix) {
int sum[2] = {0};
vector<int> s;
for(int i = matrix.begin();i < matrix.size();i++)
{
for(int j = matrix[i].begin();j < matrix.[i].size();j++)
{
if(i == j)
sum[0]+=matrix[i][j];
if((i+j) == matrix.size())
sum[1]+=matrix[i][j];
}
}
return (sum[0] + sum[1]);
}
int main()
{
vector <vector<int> > matrix = {
{2,4,6},{4,8,12},{6,12,18}
};
int dSum = diagonalSum(matrix);
return 0;
}
This code has several problems.
First, performance:
int diagonalSum(vector<vector<int> > matrix) {
The above copies the matrix with all its elements every time you call the function, for no apparent reason. Complexity: O(n2).
Better pass by reference to const (complexity O(1)):
int diagonalSum(const vector<vector<int> > & matrix) {
Then the code tries to copy an iterator object, from matrix.begin()
, which is supposed to iterate over objects of type vector<int>
, and tries to assign this iterator into an integer variable. The integer and the iterator are of incompatible types, and can't be assigned to one another, and hence the error.
I assume you wanted to use an index instead:
for(int i = 0;i < matrix.size();i++)
{
for(int j = 0; j < matrix[i].size();j++)
And even that is a bad solution, from a complexity stand-point. Your solution is O(n2), where your task can be easily performed in O(n) with one loop.
I am still not able to initialize the vector in main, kindly guide me on the issue
@taherlunawadi Works well here: godbolt.org/z/1tpDCQ (although
using namespace std;
is a very bad idea)@taherlunawadi Also note that your code is buggy. One of your diagonals is calculated incorrectly. You are summing up elements to the right of the diagonal you really want. You can either debug your code in a debugger, which is a very important lesson, or print many intermediate values you access. For example, print the values you sum-up, before adding them to the result. This will give you an insight of what is going on.
I am not able to properly extract the bug, could you please elaborate?
@TaherLunawadi debugging is usually best performed with as small example as possible. For example 1x1 matrix, or 2x2 in your case. Examine the values in the
sum
array. Is only one diagonal wrong? Are both? Check each step (single-step in a debugger, or print every step) in one buggy diagonal at a time. Debugging is part of a life of a programmer - you must master it