Well, If you are checking after each entered number (which is what it looks like) you need to compare the entered number to the answer matrix (you could have this as a variable in the program somewhere) for the indicies input by the user. Return 1 if the same, 0 if not.
There are many ways to do this. This is just one possibility.
TylerSBreton
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 25
Solved Threads: 3
One bug in your program, when the user enters x and y coordinate where he wants to put his number, you dont check to see if those coordinates are within bound (i.e. a < 9 and a > 0) and the same with b. This may allow user to write at a location which doesnt belong to him.
Also you dont check whether the number entered by the user lies in the range of 1 to 9. Perform all the bound checks properly to ensure that your program doesnt crash and perform in unexpected manner.
The check function can be something simple like: (have not checked the function, just my logic)
{
int index = 1 ;
for( index = 1; index <= 9; ++index ) {
if( matrix[row][index] == value_entered ) {
if( index == col ) {
continue ;
}
else {
printf( "The value is already present" ) ;
return 0 ; // return false, condtition not satisfied
}
}
}
for( index = 1; index <= 9; ++index ) {
if( matrix[index][col] == value_entered ) {
if( index == row ) {
continue ;
}
else {
printf( "The value is already present" ) ;
return 0 ; // return false, condtition not satisfied
}
}
}
return 1 ; // value not present already, so continue adding the new value to matrix
}
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
I use a "step" kind of logic. Each check function returns either 1 (checked ok, or 0, check failed)
if(checkRow(row, col)) {
if(checkCol(row, col)) { //only if row checks out OK, check the col
if(checkBox(row, col)) { //only if row and col check out OK, check box
number is OK
}
}
}
To speed things up a bit, I use a 1D Sudoku array, but have also used 2D Sudoku grid arrays in the past.
To handle the checkBox() logic, I use two tables. The first one simply tells me, for any sqr, what box it's in - simple. The second table tells me what 8 squares need to be checked for any box number. (one sqr is the digit being checked).
It's a little extra work to set it up, but it makes your code shorter, and your program, a bit faster.
If you have specific questions, you may want to visit: http://www.setbb.com/phpbb/?mforum=sudoku
They've forgotten more about programming Sudoku, then most of us will ever care to learn. It's not a very active board, however.
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185