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

explain the following code line by line. #include <stdio.h> #include <stdlib.h>

ID: 3732332 • Letter: E

Question

explain the following code line by line.

#include <stdio.h>
#include <stdlib.h>
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;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
//checking the availability of the given number in the puzzle with given row and column
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);//increamenting column value and calling with that new column value as parameter
else if((row+1)<9) return fillSudoku(puzzle, row+1, 0);//increamenting row value and calling that function with new row value
else return 1;
}
else
{
for(i=0; i<9; ++i)
{
if(isAvailable(puzzle, row, col, i+1)) //logic to check the avilability of the given number in perticular shell of the 2D array
{
puzzle[row][col] = i+1; //if availibale change the value to i+1
if((col+1)<9)
{
if(fillSudoku(puzzle, row, col +1)) return 1;
else puzzle[row][col] = 0; //if column+1 >= 9 then fill the array values as 0
}
else if((row+1)<9)
{
if(fillSudoku(puzzle, row+1, 0)) return 1;
else puzzle[row][col] = 0; //if row+1 >= 9 then fill the array values as 0
}
else return 1;
}
}
}
return 0;
}
else return 1;
}
// program startts executing from here only
int main()
{
int i=0, j=0;
FILE *fp; //declaring file pointer
fp = fopen("in.txt","r");//opening the text file in.txt using reading mode so that data present inside the file can be read only
FILE *fw; //declaration of another fiile pointer
fw= fopen ("out.txt", "w"); //opening the text file in write mode so that user can write into the file
if(!fp)
{
perror("Error while opening the file"); //when file pointer fp does not point to any pointer so there will be error in opening the file
exit(EXIT_FAILURE);
}
int puzzle[9][9]; //declaration of a 2D array
char ch;
while((ch = fgetc(fp))!=EOF){ //reading cahracter from the in.txt till the file pointer reach the end
if(j==9)
{
i++;
j=0;
}
puzzle[i][j] = ch-'0'; //puting number to the 2D array number= ascii value of character - 48(scii value of '0')
j++;
}

if(fillSudoku(puzzle, 0, 0))
{
fprintf(fw, " +-----+-----+-----+ "); //writting to the desktop and file
for(i=1; i<10; ++i)
{
for(j=1; j<10; ++j) fprintf(fw,"|%d", puzzle[i-1][j-1]); //printing the value of each shell of 2d array row by row
fprintf(fw,"| ");
if (i%3 == 0) fprintf(fw, "+-----+-----+-----+ ");
}
}
else fprintf(fw, " NO SOLUTION ");
fclose(fp); // closing in.txt file
fclose(fw);// closing out.txt file after the operation
return 0;
}