Thread: Need help with a Sudoku verifying program

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    9

    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. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

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

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. sudoku program
    By ElemenT.usha in forum C Programming
    Replies: 1
    Last Post: 02-12-2008, 12:51 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM