i have a 2D array 9x9 that represent Sudoku
that hold onley a number from 1 to 9 in each cell
how to write one or more functions to check whether the 9x9 board configuration is a successful Sudoku configuration; i.e., each row, each column and each of the nine 3×3 sub-grids contains exactly one of the digits from 1 to 9?
I would suggest using an array of 9 booleans that will account for the values present in the column / row you're checking.
1 2 3 4 5 6 7
int matrix[9][9];
bool appeared[9]; // initialize to false
// and inside your loop...
if (appeared[matrix[x][y]])
// (x,y) has a duplicated value.
else
appeared[matrix[x][y]] = true;
Zeillinger could you explain why you initialize tha array to false?
Before going over the elements in the column / row they haven't actually appeared, only when they're spotted in the column / row you can mark them (by setting the value of appeared[number] to be true if you encountered number).
zz77z wrote:
and why you didn't write anything in the true area of if statment?
I left this for you since you haven't specified what you want to do if duplicated values are found.