Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone take a look at my Sudoku Solver code and find out what am I doing wr

ID: 3820380 • Letter: C

Question

Can someone take a look at my Sudoku Solver code and find out what am I doing wrong because the output does not solve the game properly? Perhaps someone can make a new one from scratch if easier? Program needs to prompt user for game layout (constants and variables) and output solution in a squared box. (user will imput 0 for every variable). Thanks in advance.

#include <stdio.h>

int isAvailable(int puzzle[][9], int row, int col, int num)
{
int rowStart = (row/3) * 3;
int colStart = (col/3) * 3;
int i;

for(i=0; i<9; ++i)
{
if (puzzle[row][i] == num) return 0;
if (puzzle[i][col] == num) return 0;
if (puzzle[rowStart + (i%3)][colStart + (i/3)] == num) return 0;
}
return 1;
}

int fillSudoku(int puzzle[][9], int row, int col)
{
int i;
if(row<9 && col<9)
{
if(puzzle[row][col] != 0)
{
if((col+1)<9) return fillSudoku(puzzle, row, col+1);
else if((row+1)<9) return fillSudoku(puzzle, row+1, 0);
else return 1;
}
else
{
for(i=0; i<9; ++i)
{
if(isAvailable(puzzle, row, col, i+1))
{
puzzle[row][col] = i+1;
if((col+1)<9)
{
if(fillSudoku(puzzle, row, col +1)) return 1;
else puzzle[row][col] = 0;
}
else if((row+1)<9)
{
if(fillSudoku(puzzle, row+1, 0)) return 1;
else puzzle[row][col] = 0;
}
else return 1;
}
}
}
return 0;
}
else return 1;
}

int main()
{
int i, j ;
int puzzle[9][9] ;
char c ;
printf("Please enter layout using 0 for unknowns/variables: ") ;
for(i=0;i<9;i++)
{

   for(j=0;j<9;j++)
   {
       scanf("%c",&c);
       puzzle[i][j] = c ;

       puzzle[i][j] -= 47 ;
       if(puzzle[i][j]<0)puzzle[i][j]=0;
       }
   }

if(fillSudoku(puzzle, 0, 0))
{
printf(" +-----+-----+-----+ ");
for(i=1; i<10; ++i)
{
for(j=1; j<10; ++j) printf("|%d", puzzle[i-1][j-1]);
printf("| ");
if (i%3 == 0) printf("+-----+-----+-----+ ");
}
}
else printf(" NO SOLUTION ");

return 0;
}

Explanation / Answer

#include <stdio.h>
int puzzle[9][9];
int isAvailable( int row, int col, int num)
{
    int rowStart = (row/3) * 3;
    int colStart = (col/3) * 3;
    int i, j;

    for(i=0; i<9; ++i)
    {
        if (puzzle[row][i] == num) return 0;
        if (puzzle[i][col] == num) return 0;
        if (puzzle[rowStart + (i%3)][colStart + (i/3)] == num) return 0;
    }
    return 1;
}

int fillSudoku(int row, int col)
{
    int i;
    if(row<9 && col<9)
    {
        if(puzzle[row][col])
        {
            if((col+1)<9) return fillSudoku(row, col+1);
            else if((row+1)<9) return fillSudoku(row+1, 0);
            else return 1;
        }
        else
        {
            for(i=0; i<9; ++i)
            {
                if(isAvailable(row, col, i+1))
                {
                    puzzle[row][col] = i+1;
                    if((col+1)<9)
                    {
                        if(fillSudoku(row, col +1)) return 1;
                        else puzzle[row][col] = 0;
                    }
                    else if((row+1)<9)
                    {
                        if(fillSudoku(row+1, 0)) return 1;
                        else puzzle[row][col] = 0;
                    }
                    else return 1;
                }
            }
        }
        return 0;
    }
    else return 1;
}
void display()
{//The function to display the solved Sudoku
int row,column;
printf("THE SOLVED SUDOKU ");
for(row=0;row<9;row++)
{
for(column=0;column<9;column++)
printf("%d ",puzzle[row][column]);
printf(" ");
}

}
int main()
{
int row,column;
printf("Enter the desired sudoku and enter 0 for unknown entries ");
for(row=0;row<9;row++)
for(column=0;column<9;column++)
scanf("%d",&puzzle[row][column]);
fillSudoku(0,0);//We start solving the sudoku.
if(row>8)//If the row number is greater than 8 than we have filled all cells hence we have solved the sudoku
    display();
}