Hey guys I need some help identifying the error in my program, as I'm using nested loops to print out a 2-D array of a solved Sudoku puzzle. I was trying to figure out the issue within the loop. But I am struggling to identify the problem.
Firstly, sudoku puzzles have a third property of not having repeats within 3x3 squares as well as within rows and columns (look up sudoku if you don't know what I'm talking about).
Secondly, your algorithm will never work since you have no way of backtracking. If it paints itself into a corner, which it will inevitably do, it has no way of undoing previous choices in order to try different ones.
They randomly fill the "diagonal 3x3 boxes" first, since they don't interact with each other. From there they try different numbers in the empty locations until they're all filled.
Line 13. What makes arr[0][0] so special that it doesn't need to be checked?
Line 16. Why are you changing the value of the cell?
Line 21. The condition is always false:
1 2 3
for(int k = 0; k < r; k++) {//to check if row repeats.
for(int p = 0; p < c; p++) { //to check if column repeats.
if(arr[k][p] == arr[r][c] && (r == k || c == p)) repeats = true;
The for loops guarantee that k!=r and c!=p
Also why are you checking only the cells to the top/right of r,c?
The code to check for a valid board should be a series of loops:
1 2 3 4 5 6 7 8 9
for (each row) {
check for duplicates in the row
}
for (each column) {
check for duplicates in the column
}
for (each box) {
check for duplicates in the box
}
Work on getting just one of these checks working. Once you have one, you can apply what you learned to the others.
Also, think about how you might check for duplicate entries a single row. Can you think of an efficient way to do it? It's possible to do it with a single pass through the entries.