-5
#include <iostream>
using namespace std;
//defining 9X9 grid.

int a[9][9] ={{0,0,3,0,9,2,6,0,0},
              {1,0,0,3,0,0,8,0,0},          
              {0,0,5,0,1,0,0,4,0},
              {0,3,0,0,0,0,2,5,8},  
              {2,4,0,0,5,0,0,0,0},
              {0,0,0,6,2,0,0,0,3},
              {0,1,4,0,0,9,0,3,0},
              {6,0,0,7,0,0,1,0,0},
              {3,0,0,0,0,4,0,0,2} };

// class sudoku.

class sudoku{
   public:
   int row,col,i,j,num;

//to check presence of element in particular row.

bool rowCheck(int a[9][9],int &row,int num)
{
   for(j=0;j<9;j++)
   {
     if(a[row][j]==num)
     return true;
   }
   return false;
}

//to check presence of element in particular column.

bool colCheck(int a[9][9], int &col, int num)
{
   for(j=0;j<9;j++)
   {
     if(a[j][col]==num)
     return true;
   }
   return false;
}

//to check presence of element in particular 3X3 grid.

bool boxCheck(int a[9][9],int &row ,int &col ,int num)
{ 
  int x,y;
  if(row<3)
  x=0;
  else if(row>=3 && row<6)
  x=3;
  else
  x=6;

  if(col<3)
  int y=0;
  else if(col>=3 && col<6)
  y=3;
  else
  y=6;

  for(i=x;i<x+3;i++)
  {
    for(j=y;j<y+3;j++)
    {
        if(a[i][j]==num)
        return true;
    }
  }
  return false;
}

//to check index which is unassigned.

bool unAssigned(int a[9][9],int &row,int &col)
{
  for(row=0;row<9;row++)
  {
    for(col=0;col<9;col++)
    {
        if(a[row][col]==0){
        return true;}
    }
   }
   return false;
}

//to return true if position is suitable to insert .

bool isSafe(int a[9][9],int &row,int &col,int num)
{
  if(!rowCheck(a,row,num) && !colCheck(a,col,num) && 
    !boxCheck(a,row,col,num))
     return true;
  else
  return false;
}

//function to solve sudoku.

bool sudokuSolver(int a[9][9])
{
   if(!unAssigned(a,row,col))   
   return true;

   for(i=1;i<=9;i++)
   {
     if(isSafe(a,row,col,i))
     {
        a[row][col]=i;
        cout<<a[row][col];

        if(sudokuSolver(a))
        return true;

        a[row][col]=0;
     }
 }
 return false;
}
void display(int a[9][9])
{
  for(i=0;i<9;i++)
  {
        for(j=0;j<9;j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
  }
}
//class ends

};

//main method

int main()
{
  sudoku s;                
  s.sudokuSolver(a);
  s.display(a);
  return 0;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 05 '17 at 08:45
  • 1
    I also recommend you take some time to read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert, and learn how to use a *debugger* to catch crashes. – Some programmer dude Oct 05 '17 at 08:45
  • I think there it is: `for(i=1;i<=9;i++)` – Konstantin T. Oct 05 '17 at 08:49
  • @KonstantinT. Please elaborate. I only see that counter being used as value, not as anything which can cause a segfault. – Yunnosch Oct 05 '17 at 08:53
  • 1
    Please study and apply the concept of a [mcve]. Your quoted code is far from **M**inimal. And "Segmentation fault (core dumped) ,all suggestions are welcomed" is far from being a precise question. You need to narrow it down a little, even if you cannot use a debugger (https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb), you should be able to better pinpoint the problem. Making an MCVE actually helps with that. – Yunnosch Oct 05 '17 at 08:56
  • @Yunnosch Perhaps in folowing code you use it as array index. And when it become `9` you get index owerflow. – Konstantin T. Oct 05 '17 at 09:04
  • @KonstantinT. when `i` is used as index the loop is `for(i=0;i<9;i++)` which is fine – 463035818_is_not_an_ai Oct 05 '17 at 09:07
  • @KonstantinT. What do you mean by "perhaps" ? Please refer to a piece of code where that happens. – Yunnosch Oct 05 '17 at 09:08
  • You mix the identifiers `row` and `col`, they are class attributes and they are method parameters, pretty much everywhere. If there is the smallest inconsistency you will end up in debugging hell. Actually I think you already are... – Yunnosch Oct 05 '17 at 09:10
  • @tobi303 Yes, you are right. – Konstantin T. Oct 05 '17 at 09:13
  • 1
    Don't use member variables to save keystrokes. Use local variables for iteration. It looks like you have taken a solution with free functions and crammed it into an "object-oriented" solution. – molbdnilo Oct 05 '17 at 09:19
  • @Yunnosch well thanks for your time . I just want to say that it's my first question here ,so apologies if it caused u inconvenience.I try to be more precise next time. – Prateek Bajaj Oct 05 '17 at 10:48
  • If an answer helped you, don't forget to accept it – fjardon Oct 11 '17 at 10:48

2 Answers2

1

After calling: unAssigned(a,row,col) the value of row is 9 and the value of colis 9 when unAssigned() returns false. This is a consequence of using references to row and col.

bool unAssigned(int a[9][9],int &row,int &col)
{
  for(row=0;row<9;row++)
  {
    for(col=0;col<9;col++)
    {
        if(a[row][col]==0){
        return true;}
    }
   }
   // here: row is 9 and col is 9
   return false;
}

This means that you can return from sudokuSolver() with row and col out of bounds. This will trigger a segmentation fault in the following line:

    if(sudokuSolver(a))
        return true;
    // here row or col are equal to 9 which is out of bounds
    a[row][col]=0; // seg-fault here
fjardon
  • 7,921
  • 22
  • 31
0

You never initialize row and col which leads to undefined behaviour once you use their values.

Apart from that, I would suggest you to avoid hard coded array bounds and raw loops. If you use containers and iterators instead you can avoid out of bounds errors completely (not the problem here, but a line for(i=1;i<=9;i++) looks very suspicious and makes me think twice to realize that it is ok). Moreover, dont pass by reference if the parameter is actually not modified by the method. E.g. bool colCheck(int a[9][9], int &col, int num) does not modify col, thus it is rather confusing why it takes col as reference. Also it is confusing that both row and col are members of the class but at the same time you pass them between the methods. I would suggest to rename the members to max_row and max_col, respectively.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    I think you are close, but it would be helpful to sort the use of `row` and `col` as method parameters from the uses as object attributes. Most cases are reference parameters (which themselves cannot be uninitialised). Please elaborate where these identifiers are used before initialisation and how that causes the segfault. The referenced attributes are uninitialised, but that needs to be tracked all the way. – Yunnosch Oct 05 '17 at 09:14
  • @Yunnosch yes I just saw your comment and added a sentence. However, the main problem is still accessing unitialized values leading to UB – 463035818_is_not_an_ai Oct 05 '17 at 09:15
  • True. I fine tuned my comment. – Yunnosch Oct 05 '17 at 09:21
  • Actually I think the root cause is pretty much nailed by @molbdnilo. It seems that the semantic of the attributes col and row is unclear even to OP and that caused what this answer describes. – Yunnosch Oct 05 '17 at 09:24
  • 2
    @Yunnosch anyhow it seems like OP has lost interest and tbh so did I ;) – 463035818_is_not_an_ai Oct 05 '17 at 09:26
  • This is not the problem. `row` and `col` are initialized before use by the call to `unAssigned` which uses references as arguments – fjardon Oct 05 '17 at 10:48
  • @tobi303 thanks for ur help . I will try to run it again with the corrections. – Prateek Bajaj Oct 05 '17 at 10:50
  • @fjardon To what do you think `row` and `col` are initialised, especially after unAssigned() ? 9? They are set there, but I would not describe that as "initialised", let alone "sensibly and intentionally initialised". I think we agree on what is the root cause, though we seem to describe it differently. Your answer however is good in any case. – Yunnosch Oct 05 '17 at 11:15
  • @fjardon `sudokuSolver` is the first time where `row` and `col` are used and there it is the members that are passed to `unAssigned` before they are initialized – 463035818_is_not_an_ai Oct 05 '17 at 11:20
  • @tobi303 You are right, and `unAssigned` initializes them. I should have written: `row` and `col` are never *read* before being initialized. – fjardon Oct 05 '17 at 11:37