Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

explain the following code line by line. #include #include int isAvailable(int puzzle[][9], int row, int col, int num) { int rowStart = (row/3) * 3;

explain the following code line by line.

#include #include int isAvailable(int puzzle[][9], 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 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=0, j=0; FILE *fp; fp = fopen("in.txt","r"); FILE *fw; fw= fopen ("out.txt", "w"); if(!fp) { perror("Error while opening the file"); exit(EXIT_FAILURE); } int puzzle[9][9]; char ch; while((ch = fgetc(fp))!=EOF){ if(j==9){ i++; j=0; } puzzle[i][j] = ch-'0'; j++; } if(fillSudoku(puzzle, 0, 0)) { fprintf(fw, " +-----+-----+-----+ "); for(i=1; i<10; ++i) { for(j=1; j<10; ++j) fprintf(fw,"|%d", puzzle[i-1][j-1]); fprintf(fw,"| "); if (i%3 == 0) fprintf(fw, "+-----+-----+-----+ "); } } else fprintf(fw, " NO SOLUTION "); fclose(fp); fclose(fw); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions