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;
}