............solve sudoku

what is the error
#include
#include
using namespace std;
bool canPlace(int matrix[][9],int i, int j ,int n, int number){
for(int x=0;x<n;x++){
if(matrix[x][i]==number||matrix[i][x]==number){
return false;
}
}
//subgrid
int rn =sqrt(n);
int sx = (i/rn)*rn;
int sy = (j/rn)*rn;

for(int x=sx;x<sx+rn;x++){
    for(int y=sy;y<sy+rn;y++){
        if(matrix[x][y]==number){
            return false;
        }
    }
}
return true;

}
bool solveSudoku(int matrix[][10],int i, int j, int n){
//base case
if(i==n){
//print the matrix
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<matrix[i][j];
}
cout<<endl;
}
return true;
}
//case row ends
if(j==n){
return solveSudoku(matrix,i+1,0,n);
}
//skip the pre filled cells
if(matrix[i][j]!=0){
return solveSudoku(matrix,i,j+1,n);
}
//rec case
//fill the current cell with possible options
for(int number=1;number<=n;number++){
if(canPlace(matrix,i,j,n,number)){
//assume
matrix[i][j]=number;

        bool couldWeSolve = solveSudoku(matrix,i,j+1,n);
        if(couldWeSolve){
            return true;
        }
    }
}
//backtrack
matrix[i][j]=0;
return false;

}
int main(){

int matrix[9][9] = {5,3,0,0,7,0,0,0,0},
                   {6,0,0,1,9,5,0,0,0},
                   {0,9,8,0,0,0,0,6,0},
                   {8,0,0,0,6,0,0,0,3},
                   {4,0,0,8,0,3,0,0,1},
                   {7,0,0,0,2,0,0,0,6},
                   {0,6,0,0,0,0,2,8,0},
                   {0,0,0,4,1,9,0,0,5},
                   {0,0,0,0,8,0,0,7,9};
solveSudoku(matrix,0,0,9);

return 0;}


udpated code

what is the error in the code that i wrote?

paste the code in cb ide so that i can debug it easily

the backtrack has to be inside for() {} loop

for(int number=1;number<=n;number++){
if(canPlace(matrix,i,j,n,number)){
//assume
matrix[i][j]=number;

        bool couldWeSolve = solveSudoku(matrix,i,j+1,n);
        if(couldWeSolve){
            return true;
        }
   matrix[i][j] =  0 ;

    }
}

please share code in ide so that i can debug it entirely

my cb ide does not work

did u try hard refresh ctlr+shift+R

else paste it in some other ide

line 6
if matrix[x][ j ]==number||matrix[i][x]==number
^^^

move line 57 after line 54

add
if (mat[i][j] != 0) /// update
return false;
at start of canPlace function

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.