When I compile my program, it displays
segmentaion fault (or) segmentation fault(core dumped)
My code seems to be working sometimes it gives the number of knight moves on the whole chessBoard
, when moved on a square randomly chosen from all other paths, possibly a knight can take,but this doesn't seem to work always(just for simplicity,I have taken 5x5 chessboard ), why is this happening,what can i do to fix it?
#include <iostream>
#include <array>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
array<array<int , 5>,5> chessBoard = {};
array<int , 8> horizontal = {2,1,-1,-2,-2,-1,1,2};
array<int , 8> vertical = {-1,-2,-2,-1,1,2,2,1};
array<int, 8> temp ={};
int count = 0 , t = 0 ,index;
int cR = 4 , cC = 4 ,square = 1;
srand(static_cast<unsigned>(time(0)));
do
{
t = 0;
for(size_t i = 0;i < 8;i++ )
{
cR += vertical[i];
cC += horizontal[i];
if(cR > 0 && cR < 8 && cC > 0 && cC < 8 && chessBoard[cR][cC] != -1)
{
temp[t] = i;
t++;
}
cR -= vertical[i];
cC -= horizontal[i];
}
if(t > 0)
{
index = rand() % t;
chessBoard[cR][cC] = -1;
cR += vertical[temp[index]];
cC += horizontal[temp[index]];
count++;
}
for(size_t j=0;j< t;j++)
temp[j] = 0;
square++;
}while(square <= 25);
cout<<"count is "<<count;
return 0;
}
The error is Segmentation fault or sometimes it displays Segmentation fault (core dumped),what is wrong?