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

In C Programming: Declare a minimum size data structure to store the values of a

ID: 662631 • Letter: I

Question

In C Programming:

Declare a minimum size data structure to store the values of a Sudoko puzzle (suppose empty cells will have the value of 0 while the other cells will have values from 1 to 9).

Then ask user if he/she wants to enter data for a sudoku puzzle or a sudoku solution. Suppose user enters 1 to enter a puzzle; or 2 to enter a solution to be checked.

Write/call a function that asks user to enter the values for a Sudoku puzzle or solution and saves these values into the data structure you declared in the previous item. User is expected to enter a number between 0 and 9 for each cell in case of a puzzle. Zero is for empty cells, 1-9 are for other cells. In case of a solution, user is expected to enter only the values between 1 and 9. Your program should reject invalid values. [Note: I am sure you are wondering if you have to enter 82 values by hand. Unfortunately, yes :( until we learn how to read data files in ch3. But there is another option: you develop your program as if you will read the values from keyboard. But when you are running the program, the operating system

Explanation / Answer

#include<stdio.h>
int sudoku[9][9];
void solvesudoku(int,int);
int checkrow(int row,int num)
{
int column;
for(column=0;column<9;column++)
if(sudoku[row][column]==num)
return 0 ;
else
return 1;
}
int checkcolumn(int column,int num)
{
int row;
for(row=0;row<9;row++)
if(sudoku[row][column]==num)
return 0;
else
return 1;
}
int checkgrid(int row,int column,int num)
{
row=(row/3)*3 ;
column=(column/3)*3;
int r,c;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
if(sudoku[row+r][column+c]==num)
return 0;
return 1;
}
void navigate(int row,int column)
{
if(column<8)
solvesudoku(row,column+1);
else
solvesudoku(row+1,0);
}
void display()
{
int row,column;
printf("THE SOLVED SUDOKU ");
for(row=0;row<9;row++)
{
for(column=0;column<9;column++)
printf("%d ",sudoku[row][column]);
printf(" ");
}
getch();
}
void solvesudoku(int row,int column)
{
if(row>8)
display();
if(sudoku[row][column]!=0)
navigate(row,column);
else
{
int ctr;
for(ctr=1;ctr<=9;ctr++)
{
if((checkrow(row,ctr)==1)&&(checkcolumn(column,ctr)==1)&&(checkgrid(row,column,ctr)==1))
{
sudoku[row][column]=ctr;
navigate(row,column);
}
}
sudoku[row][column]=0; }

}
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",&sudoku[row][column]);
solvesudoku(0,0);
}