-2

Problem : I am trying to initialize a square 2d array and find the absolute difference of the sum of its diagonals. I have written following code for that purpose.

#include<vector>
#include<iostream>

using namespace std;

vector<vector<int>> init() // function to initialize the array
{
  int size;
  cin>>size;
  vector<vector<int>> vector(size);

  for(int i = 0; i < size; ++i)
  {
    vector[i].resize(size);
    for(int j = 0; j < size; ++j)
    {
      cin>>vector[i][j];
    }
  }


  return vector;
}



int diagonal_diff(vector<vector<int>> vector)// function to find difference                                                                                                               
{
  int diff;

  auto sum_1  = 0;
  auto size = vector.size();
  for(auto i  = 0; i < size; ++i)
  {
    auto j = i;
    sum_1 = vector[i][j] + sum_1;

  }

  int sum_2 = 0;
  for(auto i  = size -1; i >= 0; --i)
  {
    auto j = (size-1) - i;
    sum_2 = vector[i][j] + sum_2;

  }

  diff = sum_1 - sum_2;
  diff = (diff >= 0)? diff:diff*(-1);

  return diff;
}

int main()
{
  auto vector = init();//initialising array

  for(auto i  = 0; i < vector.size(); ++i)//printing the array
  {
    for(auto j = 0; j < vector.size(); ++j )
    {
      cout<< vector[i][j];
    }
  }

  auto temp = diagonal_diff(vector);// calling difference function
  cout<<temp<<endl;


  return 0;

}

but it gives segmentation Fault after printing the array. can't figure out what the cause is.

I was trying to solve this problem from hackerRank. If I comment out the line which calls the diagonal_diff() function then the code runs fine. So I think the error is in diagonal_diff() function.

PraAnj
  • 899
  • 1
  • 10
  • 27
Yash Sharma
  • 39
  • 1
  • 5
  • 5
    On an unrelated note, having both `vector` the class and `vector` the variable is confusing. Please read [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and try to avoid using standard class-names as variables anyway. – Some programmer dude Sep 03 '18 at 15:49
  • 1
    For your problem, I suggest you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). By using common debugging techniques it shouldn't be to hard to find where you go out of bounds (which is the most likely reason). – Some programmer dude Sep 03 '18 at 15:51
  • 4
    comparing an unsigned variable to >= 0 while decrementing it won't work. – Swordfish Sep 03 '18 at 15:56
  • @Swordfish thanks dude and sorry for wasting everyone's time won't ask such stupid questions again. – Yash Sharma Sep 03 '18 at 16:02
  • 1
    When looking for a problem with a signature like this one in your code (or anyone's code for that matter), it's safe to start with the assumption that you have an out-of-bounds access somewhere. Replace all of the calls to operator`[]` with calls to the `at` method. `at` makes a bounds check and will let you know by throwing an exception if your program wanders off into the weeds. – user4581301 Sep 03 '18 at 16:03

1 Answers1

0

inside diagonal_diff you have the following loop:

int sum_2 = 0;
for (auto i  = size - 1; i >= 0; --i)
{
    auto j = (size-1) - i;
    sum_2 = vector[i][j] + sum_2;   
}

The problem is that size declartion looks like this:

auto size = vector.size();

vector.size() returns you a size_t object type, what mean that there are no negative values.

Look again on your loop, and look on i declaration:

auto i  = size - 1

Because size is size_t type, your i will be automaticlly size_t too. Now, your loop's stop condition is: i >= 0, which means always true in case that i is size_t. When i equals to 0, and you arrive to third loop's part: --i, instead of getting -1, it gets the highest value of size_t. Then you are tring to do: sum_2 = vector[i][j] + sum_2; and getting an exception.

To solve this problem, all you have to do is to change i declaration to: int i = size - 1.

Note: it's highly not recommended to use vector as a name, especially not when you are using using namespace std; inside your code. vector is a class name, and it can lead to code conflicts.. you can use vec as name instead.

Coral Kashri
  • 3,436
  • 2
  • 10
  • 22