-1

I am trying to solve an exercise, but I'm getting a Segmentation fault.

The first part of the code consists in initializing an vector of vector.

Then there is task2. I try to print Task 2 before the while cicle but it never prints. So I guess the error is in the first part of code! Below you can find the code that you can compile.

possible input you can use :

0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

below you can find the code that you can compile.

#include <iostream>
#include <vector> 
using namespace std;
int main (){

    const int matrix_size = 5;
    int i =0,j=0,taskdone=0;
    vector<vector<int>> mat(matrix_size, vector<int>(matrix_size));

    for(i = 0;i < matrix_size; i++)
    {
        for(j = 0; j < matrix_size; j++)
        {
            cout <<"Value mat"<<mat[i][j] << "j is "<<j<< "\n";
            cin >> mat[i][j];
            
        }
        cout << endl;
         cout << "Done "<<i<<"|\n";
    }
  i =0;j=0;
  cout << "task 2 ";
  while(!mat[3][3]==1){
      cout << "task 2 ";
      if(mat[i][j]==1){
          if(i<3){
              mat[i+1][j]=1;
              mat[i][j]=0;
              taskdone++;
              i++;
              cout<< "task 1 "<<taskdone;
          }else if (i>3){
              mat[i-1][j]=1;
              mat[i][j]=0;
              taskdone++;
              i--;
              cout<< "task 2 "<<taskdone;
          }
          if(j<3){
              mat[i][j+1]=1;
              mat[i][j]=0;
              taskdone++;
              j++;
              cout<< "task 3 "<<taskdone;
          }else if (j>3){
              mat[i][j-1]=1;
              mat[i][j]=0;
              taskdone++;
              j--;
              cout<< "task 4 "<<taskdone;
          }
          
      }else{
          i++;
          j++;
          cout<<"i : "<<i << "j "<<j;
      }
      
  }
  cout<<taskdone;
  
  return 0;
}
starball
  • 20,030
  • 7
  • 43
  • 238
Singh
  • 783
  • 1
  • 6
  • 24
  • 1
    Please read [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl May 08 '20 at 12:09
  • you can use the `at` method when you are not certain that indices are valid and using a debugger would help to find the problem – 463035818_is_not_an_ai May 08 '20 at 12:10
  • I'm not convinced whatsoever that `while(!mat[3][3]==1)` is doing what you think it is. That is *not* synonymous with `while(mat[3][3] !=1)`, on the of-chance that is what you were shooting for. – WhozCraig May 08 '20 at 12:10
  • @WhozCraig i think it never arrives there because it doesn't print task 2 – Singh May 08 '20 at 12:11
  • 1
    you increment and decrement `i` and `j` but you never check that they are within bounds. I am missing a condition `i >= 0 && i < matrix_size` somewhere. – 463035818_is_not_an_ai May 08 '20 at 12:11
  • @Singh Never said it was your problem. I'm just saying I don't think that does what you think. And how would you know it isn't the problem anyway, since you never flush `cout` (your task 2 print has no trailing newline, and thus is not line-flushed prior to encountering that questionable loop). – WhozCraig May 08 '20 at 12:12
  • @idclev463035818 it exit the cicle when 1 is in the middle of the matrix , that's why i dont check i and j – Singh May 08 '20 at 12:15
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl May 08 '20 at 12:17
  • `task 2 i : 5j 5` is the last thing sent to stdout before the wheels fall off. Think about that for a minute. That's a 5x5 vector of vectors. Which means 0..4 are the only viable indexes. – WhozCraig May 08 '20 at 12:18
  • when inside the loop you always take the `else` branch then both are incremented until infinity. Things can go wrong, even your own logic, and making sure that indices are within bounds is an easy and efficient way to catch errors – 463035818_is_not_an_ai May 08 '20 at 12:21

1 Answers1

6

You need to learn to use the debugger, as it will very quickly tell you where it segfaulted.

Program received signal SIGSEGV, Segmentation fault.
main () at x.cpp:25
25        if(mat[i][j]==1){

(gdb) p i
$1 = 5
(gdb) p j
$2 = 5

You are attempting to access outside the bounds of mat.

mat[i][j]==1 is not true for the 0 inputs so its going to the else

else{
      i++;
      j++;
      cout<<"i : "<<i << "j "<<j;
  }

Which is quickly putting it outside the bounds of mat. Its not printing task 2, I, j etc as its being line buffered - If you had a \n in the print outs it would have shown them.

lostbard
  • 5,065
  • 1
  • 15
  • 17