Thursday, July 30, 2009

Help in writing and reading in a Sudoku Grid for C?

Hi, I'd like to know how to write a C function where upon running, displays a sudoku grid. Like this:





1 2 3 . . . 7 8 9


4 5 6 . . . 1 2 3


7 8 9 1 2 3 4 5 6


2 1 4 3 6 5 8 9 7


3 6 5 8 9 7 2 1 4


8 9 7 2 1 4 3 6 5


5 3 1 6 4 2 9 7 8


6 4 2 9 7 8 5 . 1


9 7 8 5 3 1 6 4 2





The values of each cells can be anything, this is just the format. Then, given that grid, I need to write another function that replaces all the cells in that grid with characters read from standard input. EMPTY_VALUE means blank cell, digits stand for themselves, and all other characters ignored.


So can anyone give me an example of how to display a 9x9 grid using one-dimensional arrays, then how to read in user input into each element of the arrays to fill in the grid. You could give a similar example (3x3 grid) for reference if you think I'm cheating. I've tried other web links but they're too advanced. I just need to write these 2 functions, don't just post external links. Please, I need help! Thanks!

Help in writing and reading in a Sudoku Grid for C?
to store it in a one-dimensional array, you have to create an array that is 9x9, or 81 length. To print and collect input, use 2 nested for loops, each %26lt; 9. the formula you would use to get the correct square is (9i + j + 1). with this formula you can format it like a real sudoku. good luck!
Reply:I did this for my C++ class. All you need is to use nested loops such as this pseudo-code:





for( int i=0;i%26lt;9; i++)


{


for ( int k=0; k%26lt;9; k++ )


{ cout%26lt;%26lt;" "%26lt;%26lt;value(i,k)%26lt;%26lt;" ": }


cout%26lt;%26lt;endl;


}





Here I've numbered the rows and columns from 0.


No comments:

Post a Comment