I am trying to write a program for the game sudoku. I have the following function to check each row, column must contain each value in {1..9} exactly once. But I have trouble in doing so. I cannot find my error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// array row[] is of length at least 9
bool sudoku_row_ok(int row[]) {
int sum=0;
int i;
for(i=0; i<9; i++) {
if(row[i] < 1 || row[i] > 9)
returnfalse; // out of range
sum += row[i];
}
if(sum == 45) // checking for 1+2+3+4+5+6+7+8+9 = 45
returntrue;
elsereturnfalse;
}
// array row[] is of length at least 9
bool sudoku_row_ok(int row[])
{
vector<int> v(row, row + 9);
for (int num = 1; num <= 9; num++) // test each value between 1 and 9
{
if (std::find(v.begin(), v.end(), num) == v.end()) // not found
returnfalse;
}
returntrue;
}