# Thread: 2D Array Checker Help

1. ## 2D Array Checker Help

Hey all,

lately i've been addicted to sudoku games, so i thought i would try my hand at writing a little console app that could check solutions to see if they're valid..

i'm having a lot of trouble understanding two things, mainly file io and 2D arrays..

i want to have a file eg. "sudoku.in" which contains a 9x9 grid representing the sudoku map;

023456789
204567891
340678912
456089123
567801234
678910345
789123056
891234507
912345670

and parses the values into a 2D array, where 0's denote spaces, and should be ignored of duplicated..

i think i've got this going here

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

int main(){
int nSudokuArray[9][9];
FILE* fin;
int i, j, ch;
if(!(fin=fopen("sudoku.in","r"))){
printf("Error opening file\n");
return -1;
}
for(i=0; i < 9 && !feof(fin); ++i){
for(j=0; j < 9 && !feof(fin); ++j){
nSudokuArray[i][j]=fgetc(fin);
if(nSudokuArray[i][j] == '\n'){
return -1;
}
}
ch=fgetc(fin); /* discard '\n' on the line */
}
fclose(fin);

for (row = 0; row < 9; row++)
{

}

//check columns

for (col = 0; col < 9; col++)

{

}

return 0;
}```

but im having trouble comprehending how to treat the array, and code to scan each row and collumn and return positions eg (1,1) for top left to know where it is wrong..

im just lost as to how to implement this atm, and everything i read seems to confuse me moreso :/

2. I'm not sure what you mean by "How to treat the array." First, just walk through each row of the array, and read across horizontally, seeing if you have 1-9. Then do the same vertically. That's a start.

If you don't know for sure how to do it, start by walking through the array and simply printing it.

Oh, and there is no zero in that game.

Quzah.

3. hehe. i figure if i treat 0's as spaces, it will work independant of whether it is an incomplete solution or not, again not sure how to approach that in code, ignoring the 0 and searching for repeated 1thru9's..

i've tried just having printf nSudokuArray, but it seems to be failing, so i think theres something b0rked in my file io code..

4. For starters..

This
Code:
`  nSudokuArray[i][j]=(fgetc(fin)- 48);`
Code:
` nSudokuArray[i][j]=fgetc(fin);`

5. There really isn't any need. It's just as easy to test if it's a '1' as it is a 1. Also, if you are going to do it that way, you should be doing it like this:
Code:
`nSudokuArray[i][j] = fgetc( fin ) - '0';`
Guarinteed portability.

Quzah.

6. ok, missing something here... with this code:-

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

int main(){
int a[9][9];
FILE* fin;
int i, j, ch;
if(!(fin=fopen("sudoku.in","r"))){
printf("Error opening file\n");
return -1;
}
for(i=0; i < 9 && !feof(fin); ++i){
for(j=0; j < 9 && !feof(fin); ++j){
a[i][j] = fgetc( fin ) - '0';
if(a[i][j] == '\n'){
return -1;
}
}
ch=fgetc(fin); /* discard '\n' on the line */

for (i=0; i < 9; ++i)
{
for (j=0; j < 9; ++j)
printf("a[%d][%d]=%d ", i, j, a[i][j]);
}
}

fclose(fin);
/* do what ever with the 2d array */

return 0;
}```
the out

it reads in the first row from sudoku.in correctly, from then on the numbers are like 398934939... wheres it going wrong? is it incorrect the way i have numbers in sudoku.in