-3

I have Sudoku checker assignment for my class and my task is to be able to create a code that will ask how many puzzles the user would like to solve then they'll input all of the numbers themselves. After they input all the numbers, the output would be either "Yes" or "No" in response to if the puzzle(s) are correct. So far I've been able to manipulate code to let the user input all of the numbers but I'm stuck on how to be able to check if each number in the same row, column, and 3x3 square are not repeated. Any tips on how to help me get started on checking would be grateful because I'm completely stumped on this part.

Heres my code so far

#include <stdio.h>
#define COL 9

int main (void) 
{
int n, i, j,array[100][COL];
int check=0;

scanf("%d", &n); //Enter how many puzzles you want to solve

//For loop that goes through every position in the puzzle(s)
for(i=0;i<n*9;i++)
{
  for(j=0;j<COL;j++)
    {
        //array[i][j]=0;
        //printf("Array[%d][%d]=%d\n", i,j,array[i][j]);
    scanf("%d", &array[i][j]); //User entry for puzzle(s)
    }
}
return 0;
}
Perez
  • 21
  • 1
  • 6

2 Answers2

1

First, you don't need an array of size 100.

Then, what's your problem? First things first, you have to read the input from the user, which you are basically doing.

Afterward, you have to perform 3 checks:

  • For each line / column:
    • Each item in a given line has to be uniq
    • Each item in a given column has to be uniq
  • For each "sub array" (which you know statically, don't try to be fancy here), you must apply the same rule.

If you show some code, we'll help you trying to figure out what's wrong. Otherwize, go and get this done.

Aif
  • 11,015
  • 1
  • 30
  • 44
0

the current method of declaring the arrays (as a 2d matrix) has a problem.

Suggest declaring a 3d array: int array[numArrays][9][9];

then cycle through those arrays via 3 levels of nested loops.

outer loop for each separate array, 
middle loop for each row, 
inner loop for each column.    

Even so, it is doubtful the user will want to make 81 entries times the number of arrays.

If the user is to input completed suduku arrays, that may be necessary,

however if the user is to input only initial values for unsolved arrays,

it might be much better if the user could enter row,column,value.

Still that is a lot of (error prone) entries by the user.

It may be very worth while to let the user fill in a data file (or files) and the program be able to quickly read in the file.

such a file might look similar to this:

1          <-- array number
12_5_7_9_  <-- value for first row columns
etc        <-- value for second through ninth row columns
           <-- when _ means entry empty
           <-- blank line between array initial values
user3629249
  • 16,402
  • 1
  • 16
  • 17