2D Array Checker Help

This is a discussion on 2D Array Checker Help within the C Programming forums, part of the General Programming Boards category; Hey all, lately i've been addicted to sudoku games, so i thought i would try my hand at writing a ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    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'){
                    printf("Error reading from file\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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    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. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    For starters..

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

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    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'){
                    printf("Error reading from file\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

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Move your output loop set outside of your input loops set. Watch your braces.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    ahh doh! thanks, well at least the fileio part is sorted now..

    onto scanning

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 04:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 09:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21