-3

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;
}
axelonet
  • 113
  • 5
  • Oh my... unconditional `goto`s from inside while loops? I can't even tell what it is supposed to achieve.. – Eugene Sh. Nov 11 '16 at 20:25
  • 1
    If no error is described and localized, it would be better to summit your source code to [Code Review](http://codereview.stackexchange.com/). – J. Piquard Nov 11 '16 at 20:30
  • Please replace your incr and goto with loops for i and j. – Werner Henze Nov 15 '16 at 16:54
  • Your code is walking up to the first unsolved a[i][j], then setting it to x and then terminating. There is no backtracking in your code. As said: please change your goto to propers loops. That would really help you (and others) understand your code better! – Werner Henze Nov 15 '16 at 17:00

1 Answers1

0

Your program exits after inserting a value in the matrix. After setting the value of a position in Line 41: a[i][j] = x;, you need to increment your i or j accordingly and then call goto incr;.

PS.: Learn to use simple printf() statements, they are great debuggers.

Bakhtiar Hasan
  • 889
  • 10
  • 25