0

I have tried writing the algorithm by referring this.
But i am getting Error of StackOverFlow.
Help me out to find what is wrong in the program? Is it the recursion part?

public void beginSolving(int board[][],int x,int y){
    int i = 1;
    if(unassignedCell(board,x,y)){
        board[x][y] = i;
        if(isValidCell(board,y,x,i)){
            board[x][y] = i;
        }
        if(!isValidCell(board,y,x,i)){
            board[x][y] = 0;
            i++;
        }
    } else { 
        while(x<9){
            beginSolving(board,x++,y);
            if(x==9){
                x = 1;
                beginSolving(board,x,y++);
                if(y==9){

                }
            }
        }
    }
}

1 Answer 1

3

Look at the recursive calls: beginSolving(board,x++,y). The x and y parameters are the same as in the original call (remember that the value of x++ is the value before it is incremented). Thus it is likely that you enter a never ending recursion.

The while(x < 9) will never terminate since whenever x reaches 9 it is reset to 1.

The algorithm never tries to set a number other than 1 into the board.

There may be other problems with the code as well, I did not check it in detail.

Sign up to request clarification or add additional context in comments.

3 Comments

I am a complete beginner so if possible can you help me out with getting the solution. Should i make it ++x?
Yes, ++x is better.
@MärmîkŠhâh StackOverflow errors are a great way to practice your debugging skills. You should set a breakpoint at the start of your method and run through your application and see how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.