Please explain the code line by line with comments. #include <stdio.h> int isAva
ID: 3734213 • Letter: P
Question
Please explain the code line by line with comments.
#include <stdio.h>
int isAvailable(int puzzle[][9], int row, int column, int num)
{
int rowStart = (row/3) * 3;
int colStart = (column/3) * 3;
int i, j;
for(i=0; i<9; ++i)
{
if (puzzle[row][i] == num) return 0;
if (puzzle[i][column] == num) return 0;
if (puzzle[rowStart + (i%3)][colStart + (i/3)] == num) return 0;
}
return 1;
}
int fillSudoku(int puzzle[][9], int row, int column)
{
int i;
if(row<9 && column<9)
{
if(puzzle[row][column] != 0)
{
if((column+1)<9) return fillSudoku(puzzle, row, column+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, column, i+1))
{
puzzle[row][column] = i+1;
if((column+1)<9)
{
if(fillSudoku(puzzle, row, column +1)) return 1;
else puzzle[row][column] = 0;
}
else if((row+1)<9)
{
if(fillSudoku(puzzle, row+1, 0)) return 1;
else puzzle[row][column] = 0;
}
else return 1;
}
}
}
return 0;
}
else return 1;
}
int main()
{
int i, j;
int puzzle[9][9]={{0, 0, 0, 0, 0, 0, 0, 9, 0},
{1, 9, 0, 4, 7, 0, 6, 0, 8},
{0, 5, 2, 8, 1, 9, 4, 0, 7},
{2, 0, 0, 0, 4, 8, 0, 0, 0},
{0, 0, 9, 0, 0, 0, 5, 0, 0},
{0, 0, 0, 7, 5, 0, 0, 0, 9},
{9, 0, 7, 3, 6, 4, 1, 8, 0},
{5, 0, 6, 0, 8, 1, 0, 7, 4},
{0, 8, 0, 0, 0, 0, 0, 0, 0}};
if(fillSudoku(puzzle, 0, 0))
{
for(i=1; i<10; ++i)
{
for(j=1; j<10; ++j)
printf("%d ", puzzle[i-1][j-1]);
printf(" ");
}
}
else printf(" not possilbe to solve ");
return 0;
}
Explanation / Answer
#include <stdio.h>
int isAvailable(int puzzle[][9], int row, int column, int num)//this is method to get help check available the puzzle values
{
int rowStart = (row/3) * 3; // row start value divide and multiply
int colStart = (column/3) * 3;//column start value divide and multiply
int i, j;
for(i=0; i<9; ++i) //for loop till i value 8
{
if (puzzle[row][i] == num) return 0;//calling puzzle by passing double-dimensional value passing
if (puzzle[i][column] == num) return 0;//calling puzzle by passing double-dimensional value passing
if (puzzle[rowStart + (i%3)][colStart + (i/3)] == num) return 0;
}
return 1;
}
int fillSudoku(int puzzle[][9], int row, int column)
{
int i; //int i varailble declaration
if(row<9 && column<9)// if condition fo inside loog row & colomn value less than 9 only or else it will print 1
{
if(puzzle[row][column] != 0) //double-dimensional array loop condition
{
if((column+1)<9) return fillSudoku(puzzle, row, column+1);//if column value less than 9 again it execute fillSudoku()
else if((row+1)<9) return fillSudoku(puzzle, row+1, 0);//esle if row value less than 9 again it execute fillSudoku()
else return 1; // else return 1
}
else
{
for(i=0; i<9; ++i) // for loop will execute till 8 times
{
if(isAvailable(puzzle, row, column, i+1)) // calling isAvailable by passing the puzzle , row, column and i+1 values
{
puzzle[row][column] = i+1; // double-dimensional value passing
if((column+1)<9)//if column value less than 9 it go inside the loop
{
if(fillSudoku(puzzle, row, column +1)) return 1;//calling fillSudoku by passing the puzzle , row, column and i+1 values
else puzzle[row][column] = 0;//calling fillSudoku by passing the puzzle , row, column and i+1 values
}
else if((row+1)<9)
{
if(fillSudoku(puzzle, row+1, 0)) return 1;//calling fillSudoku by passing the puzzle , row, column and i+1 values
else puzzle[row][column] = 0;//calling fillSudoku by passing the puzzle , row, column and i+1 values
}
else return 1;
}
}
}
return 0;
}
else return 1;
}
int main() // This is main method which will start the program execution
{
int i, j; // int varailble declaration
int puzzle[9][9]={{0, 0, 0, 0, 0, 0, 0, 9, 0},
{1, 9, 0, 4, 7, 0, 6, 0, 8},
{0, 5, 2, 8, 1, 9, 4, 0, 7},
{2, 0, 0, 0, 4, 8, 0, 0, 0},
{0, 0, 9, 0, 0, 0, 5, 0, 0},
{0, 0, 0, 7, 5, 0, 0, 0, 9},
{9, 0, 7, 3, 6, 4, 1, 8, 0},
{5, 0, 6, 0, 8, 1, 0, 7, 4},
{0, 8, 0, 0, 0, 0, 0, 0, 0}};// int puzzle declare and initialation
if(fillSudoku(puzzle, 0, 0)) // this is method we are using for actual solution help to us.
{
for(i=1; i<10; ++i) //for loop execute till i value 10 time and printing the printf line
{
for(j=1; j<10; ++j)
printf("%d ", puzzle[i-1][j-1]); // if i value 5 it will minus 1 means 4 it will print
printf(" "); //break the line prinlt next line
}
}
else printf(" not possilbe to solve "); //else statement to if is not valied
return 0;
}