Thread: Need help with a Sudoku verifying program

1. Need help with a Sudoku verifying program

What I can see that we are dealing with is that I need to use multidimensional arrays.

I'm suppose to verify an input file with sudoku solutions, "sudoku-solution.c" is correct or not.

The input file is the following:

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8

So far what I have is

Code:
```#include <stdio.h>

FILE *fp;

int main()

{
int i, sudoku[9][9];

fp = fopen("sudoku-solution.txt","r");

for(i=1;i<10;i++)
{
fscanf(fp,"%d", &sudoku[9][i]);
}

for(i=1;i<10;i++)
printf("%d", sudoku[2][i]);
}```
I did this to test what my values I'm scanning are. I'm just getting the first row, any tips on how I should go about doing this? I'm think that I should verify that each row vertically and horizontally add up to 45. Is there a way to print values vertically? Also is there a way to the values in the next row?

Thanks

2. I'm not sure if adding up to 45 is a good enough test.
While

1+2+3+4+5+6+7+8+9 = 45,

so does

1+5+5+5+5+6+6+6+6 = 45

3. Make three functions:

1. Test a row for 1 through 9.
2. Test a column for 1 through 9.
3. Test a 3x3 block for 1 through 9.

Quzah.

4. Testing a group (whether row, column or box of 9), tallying to 45 is not enough. You could have a 0 and a 10 value, and be missing the 1 and 9.

Say you have a small digit[] array. As you scan the row (for instance), if a square has a 4, you add one to the value at digit[4]. At the end of your 9 square group, your digit[index] should all be == 1, except digit[0], of course.

How many elements will digit[] need so it can test for 0 - 9? (not 9)

You'll need to do this test for each row, column and box, 27 tests in all, for a complete puzzle to be validated.

You can print vertically, but it's not so easy. You never need to print vertically for Sudoku. Just plan your rows, before you print. A little planning goes a long long way when it comes to printing.