-1

This code should produce a solved sudoku matrix, however the while statement puts it in an infinite loop. Removing the while statement gives me a matrix with some values still 99 or 0. And i can't generate 9 random numbers uniquely one by one. IF YOU WANT TO RUN AND CHECK THE CODE, REMOVE THE WHILE STATEMENT.

int a[9][9];
int b[9][9];

int inputvalue(int x, int y, int value) //checks horizontally, vertically and 3*3matrix for conflicts


{

    int i, j;

    for (i = 0; i < 9; i++)
    {
       if (value == a[x][i] || value == a[i][y])
          return 0;
    }

    for (i = (x / 3) * 3; i <= ((x / 3) * 3) + 2; i++)
    {
       for (j = (y / 3) * 3; j <= ((y / 3) * 3) + 2; j++)
          if (b[i][j] == value)
         return 0;
    }
    return value;
}

int main()
{
    int i, j, k;
    unsigned int s;
    cout << "sudoku\n";
    time_t t;

    s = (unsigned) time(&t);
    srand(s);
    for (i = 0; i < 9; i++)
    {
       for (j = 0; j < 9; j++)
         a[i][j] = 99;
    }

    for (i = 0; i < 9; i++)
    {
       for (j = 1; j <= 9; j++)//j is basically the value being given to cells in the matrix while k assigns the column no. 
       while(a[i][k]==99||a[i][k]==0)
       {
          k = rand() % 9;
          a[i][k] = inputvalue(i, k, j);
       }
    }

    for (i = 0; i < 9; i++)
    {
       for (j = 0; j < 9; j++)
       {
           cout << a[i][j] << "    ";
       }
       cout << endl;
    }

    return 0;
    getch();
}
  • 3
    Indentation would help make this code more readable. – Shafik Yaghmour Jul 24 '13 at 11:55
  • Side note: Your algorithm looks wrong to me. You are trying to fill the array by random numbers. What appends if your previous choice are incompatible (ie: they left you no choice for a latter box). Then I think you will get again in an infinite loop. You need some how to backtrack (ie: undo some wrong choices). Think about what happens if I give you a Sudoku with no solution. – hivert Jul 24 '13 at 12:09

1 Answers1

2

You are using assignment =, instead of equality == here:

 while(a[i][k]=99||a[i][k]=0)
              ^           ^

this should be:

 while(a[i][k]==99||a[i][k]==0)

a[i][k]=99 will always evaluate to true since 99 is non-zero, although your original code does not compile for me under gcc as it is, so I suspect the code you are running either has some parenthesizes or is slightly different.

Also using k in the while loop before it is initialized is undefined behavior and it is unclear that your termination logic makes sense for a k that is constantly changing for each loop iteration.

Another source of the infinite loop is inputvalue which seems to get stuck returning 0 in some instances, so you need to tweak that a bit to prevent infinite loops.

Also, srand(time(NULL)); is a more common way to initialize the pseudo-random number generator

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • The j loop is correct since it assigns value to the cells in the matrix and I do not need a 0 value. And using the equality operator does not help, still goes in infinte loop. – Karan Singh Jul 24 '13 at 12:35
  • I have added the code removing the while statement, try running it now. It should work. – Karan Singh Jul 24 '13 at 12:36
  • @KaranSingh Updated answer, although I see you removed the `while` loop from the question code, the question does not make sense anymore. If you want to make updates to the question you should append to the end of question instead of alter it since future readers will be completely confused. – Shafik Yaghmour Jul 24 '13 at 12:47
  • Yep, I added it back. I am new here, just getting used to the website. – Karan Singh Jul 24 '13 at 12:55