title: C Sudoku solving program
body: <p>I am trying to write a simple program solving Sudoku. It compiles, but does not work. I suspect (from debugging) the function move() causes problem, but I can't identify the bug exactly. Program is looped and although it does load Sudoku either from file or keyboard input, it seems that array filled with zeros is passed to function SolveSudoku - that is my guess. Could you take a look at my code? Thanks in advance!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int sudoku[9][9];
  int SolveSudoku(int, int);
  
  /* Function checks, if in given row the number already exists */
  int CheckRow(int row, int liczba)
  {
      int column;
      for (column = 0; column &lt; 9; column++)
      {
          if (sudoku[row][column] == liczba)
              return 0;
      }
      return 1;
  }
  
  /* Function checks, if in given column the number already exists */
  int CheckColumn(int column, int liczba)
  {
      int row;
      for (row = 0; row &lt; 9; row++)
      {
          if (sudoku[row][column] == liczba)
              return 0;
      }
      return 1;
  }
  
  /* Function checks, if in given grid the number already exists */
  int CheckGrid(int row, int column, int liczba)
  {
      row = (row/3)*3;
      column = (column/3)*3;
      int i = 0;
      int j = 0;
      for (i = 0; i &lt; 3; i++)
      {
          for (j = 0; j &lt; 3; j++)
          {
              if (sudoku[row + i][column + j] == liczba)
                  return 0;
          }
      }
      return 1;
  }
  
  /* If grid is filled, function moves to another grid */
  
  void Move(int row, int column)
  {
      if (column &lt; 8)
          SolveSudoku(row, column + 1);
      else
          SolveSudoku(row + 1, 0);
  }
  
  int Print(int array[9][9])
  {
      int row, column;
  
      for (row = 0; row &lt; 9; row++)
      {
          if ( row == 3 || row == 6)
          {
              printf("=======================\n");
          }
          for (column = 0; column &lt; 9; column++)
          {
              printf("%d ", array[row][column]);
              if (column == 2 || column == 5)
              {
                  printf("|| ");
              }
          }
          printf("\n");
      }
          return(0);
  }
  
  int SolveSudoku(int row, int column)
  {
  /* If row &gt; 8, then Sudoku is solved */
      if (row &gt; 8)
      {
          Print(sudoku);
          return 0;
      }
  /* If in given grid there is already a number different from 0, we move */
      if (sudoku[row][column] != 0)
      {
          Move(row, column);
      }
  /* If there is 0, it is filled with number and then move */
      else
      {
          int licznik;
          for (licznik = 1; licznik &lt;= 9; licznik++)
          {
              if ((CheckRow(row, licznik) == 1) &amp;&amp; (CheckColumn(column, licznik) == 1) &amp;&amp; (CheckGrid(row, column, licznik) == 1))
              {
                  sudoku[row][column] = licznik;
                  Move(row, column);
              }
          }
          sudoku[row][column] = 0;
      }
      return 0;
  }
  
  int main()
  {
      int sudoku[9][9];
      int row, column;
      int option;
      char file[100];
      printf("This is Sudoku Solver.\nChoose one of the option:\n");
      printf("1. Enter Sudoku from keyboard\n2. Load Sudoku from file\n3. Exit program\n");
      scanf("%d", &amp;option);
  
      switch (option)
      {
          case 1:
              printf("Enter Sudoku. For blank spaces enter 0. Separate numbers with space.\n");
              for (row = 0; row &lt; 9; row++)
              {
                  for (column = 0; column &lt; 9; column++)
                  {
                      scanf("%d", &amp;sudoku[row][column]);
                  }
              } 
              printf("Loaded Sudoku:\n");
              Print(sudoku);
              break;
          case 2:
              printf("Enter filename.\nFilename: ");
              scanf("%99s", file);
              FILE *fp = fopen ( file, "r" );
              if ( fp == NULL )
              {
                  printf("Couldnt open fil: %s.\nAre you sure that the program and file are in the same folder?\n\n", file);
                  exit(EXIT_FAILURE);
              }
  
              while (!feof(fp))
              {
  
                  for (row = 0; row &lt; 9; row++)
                  {
                      for (column = 0; column &lt; 9; column++)
                      {
                          fscanf(fp, "%d ", &amp;sudoku[row][column]);
                      }
                  }
  
              }
              fclose(fp);
              printf("Loaded Sudoku:\n");
              Print(sudoku);
              break;
  
          case 3:
              exit(0); break;
  
          default:
              printf("\nOption not recognized. Exit.\n"); break;
      }
      printf("\nDo you want to solve displayed Sudoku? Y \\ N");
      char askSolve;
      scanf("%s", &amp;askSolve);
      switch (askSolve)
      {
          case 'T': 
              printf("\nSolved Sudoku:\n");
              SolveSudoku(0,0);
              break;
          case 'N':
              printf("Goodbye\n");
              exit(0);
              break;
          default:
              printf("Option unknown. Goodbye.\n");
              break;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Also, some random Sudoku to enter:</p>
  
  <pre><code>9 0 0 2 3 7 6 8 0
  0 2 0 8 4 0 0 7 3
  8 0 7 1 0 5 0 2 9
  0 0 4 5 9 8 3 0 0 
  2 0 0 0 0 1 0 0 6
  5 1 0 0 0 0 0 4 7
  4 0 1 3 0 6 2 9 5
  0 5 0 9 1 0 7 3 8
  3 0 8 0 5 0 0 0 0 
  </code></pre>