I had a feel to solve sudoku using C programming in early October, and soon I came to know its no easy task. Currently, I did write a code to do all the functioning but there seems to be an error somewhere which is preventing the desired result to get printed or even get evaluated.
I use the algorithm provided here as a reference to develop my code,
This is the code which I have written and correcting the error would be greatly appreciated
#include <stdio.h>
int row(int i,int j,int a[9][9]);
int column(int i,int j,int a[9][9]); //function declaration's
int grid(int i,int j,int a[9][9]);
int main()
{
int a[9][9];
int i,j,x;
for (i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
a[i][j]=0;/*for making all the array values = 0*/
/*So that the stored garbage values will be removed*/
}
}
/*Entering known elements*/
printf("Enter the elements of known elements leaving the unknown as 0\n");
for (i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
scanf("%d",&a[i][j]);
}
}
//If a given grid is '0', lets assign a value 1 and if 1 exists in the same row or column or in ints correspond 3x3 box, increment the value upto 9
i=0,j=0,x=1;
incr:
if ( a[i][j] == 0)
{
a[i][j] = x;
if( row(i,j,a) && column(i,j,a) && grid(i,j,a) )
{
a[i][j] = x;
}
else
{
x++;
if ( x>9)
x = 1;
}
}
else
{
while(i < 9)
{
i++;
goto incr;
}
if (i == 9)
{
i =1;
j++;
goto incr;
}
if (j == 9)
{
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
}
}
int row(int i,int j,int a[9][9])
{
int m;
for(m=0;m<9;m++)
{
if( m != j)
{
if(a[i][j] == a[i][m])
{
return 0;
}
}
}
return 1;
}
int column(int i,int j, int a[9][9])
{
int m;
printf("%d%d",i,j);
for(m=0;m<9;m++)
{
if ( m != i)
{
if(a[i][j] == a[m][j])
{
return 0;
}
}
}
return 1;
}
int grid(int i,int j, int a[9][9])
{
int m,n;
if( i>=0 && i<=2 && j>=0 && j<=2) /* grid 1*/
{
for(m=0;m<=2;m++)
{
for(n=0;n<=2;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=0 && i<=2 && j>=3 && j<=5) /* grid 2*/
{
for(m=0;m<=2;m++)
{
for(n=3;n<=5;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=0 && i<=2 && j>=6 && j<=8) /* grid 3*/
{
for(m=0;m<=2;m++)
{
for(n=6;n<=8;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=3 && i<=5 && j>=0 && j<=2) /* grid 4*/
{
for(m=3;m<=5;m++)
{
for(n=0;n<=2;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=3 && i<=5 && j>=3 && j<=5) /* grid 5*/
{
for(m=3;m<=5;m++)
{
for(n=3;n<=5;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=3 && i<=5 && j>=6 && j<=8) /* grid 6*/
{
for(m=3;m<=5;m++)
{
for(n=6;n<=8;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=6 && i<=8 && j>=0 && j<=2) /* grid 7*/
{
for(m=6;m<=8;m++)
{
for(n=0;n<=2;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=6 && i<=8 && j>=3 && j<=5) /* grid 8*/
{
for(m=6;m<=8;m++)
{
for(n=3;n<=5;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=6 && i<=8 && j>=6 && j<=8) /* grid 9*/
{
for(m=6;m<=8;m++)
{
for(n=6;n<=8;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
return 0;
}