Hi all, A 1st year comp engin student here >_<... and very noob in C programming. Hope someone could help me solve or enlighten me on how to solve the following problem Your task is to read in a Sudoku grid (can be a partially completed or a fully completed grid) from a text file (with 0 to indicate blank cell), decide if the given grid is still valid, i.e. contains no repeated number! If it is valid, print the grid in a specified format, otherwise, print “Invalid Grid\n”. The following in the template of the program: Code: [/SIZE][/FONT] [FONT=Calibri][SIZE=3]#include <stdio.h>[/SIZE][/FONT] [FONT=Calibri][SIZE=3]int main() {[/SIZE][/FONT] [FONT=Calibri][SIZE=3]int sudoku[9][9];[/SIZE][/FONT] [FONT=Calibri][SIZE=3]read(sudoku);[/SIZE][/FONT] [FONT=Calibri][SIZE=3]if (valid(sudoku))[/SIZE][/FONT] [FONT=Calibri][SIZE=3]display(sudoku);[/SIZE][/FONT] [FONT=Calibri][SIZE=3]else[/SIZE][/FONT] [FONT=Calibri][SIZE=3]printf("Invalid Grid\n");[/SIZE][/FONT] [FONT=Calibri][SIZE=3]return 0;[/SIZE][/FONT] [FONT=Calibri][SIZE=3]} (P.S) i am also given a sample suduko text file which is as follows: 0 4 2 9 0 8 0 0 0 0 6 0 0 3 0 0 2 7 0 0 5 0 2 7 0 0 6 0 0 8 4 0 0 0 3 0 0 0 7 0 1 0 6 0 0 0 9 0 0 0 5 1 0 0 5 0 0 8 4 0 9 0 0 2 8 0 0 7 0 0 6 0 0 0 0 6 0 9 2 1 0 (* 3 spacing along the row and 1 spacing along each of the 3 coluumn) I have basicially 3 main step to complete. 1st step (Read the 9x9 grid): Create a void function ‘read’ that takes in a 2‐D array of size 9x9 as input (passed by reference). Function ‘read’ opens and reads in a text file named “sudoku.txt” that contains 9x9 integers representing digit [0..9]. Blank cells in Sudoku grid are indicated with a ‘0’. Fill in the 2‐D array, and since this array is passed ‘by reference’, then the updated content will be reflected in the function that calls ‘read’ (in this case, in the ‘main’ function). 2nd step (check validity of the given suduko grid): As we know, each row, column, and 3x3 block of a valid Sudoku grid CANNOT have repeated number! You are guaranteed that the file “sudoku.txt” will never contains any other integer other than [0..9]. Your task is to create function ‘valid’ that takes in the 9x9 grid read using function ‘read’ above, and then check if it is valid (that is, no repetition of [1..9], although it is ok to see a repetition of zeroes that indicates blank cells). If it is valid, returns 1, otherwise, returns 0. The final step will be(Display the suduko grid): If the given Sudoku grid is valid, then print it nicely to screen as shown below for the given [FONT= ]“[/FONT]sudoku.txt” ------------------------- | 0 4 2 | 9 0 8 | 0 0 0 | | 0 6 0 | 0 3 0 | 0 2 7 | | 0 0 5 | 0 2 7 | 0 0 6 | ------------------------- | 0 0 8 | 4 0 0 | 0 3 0 | | 0 0 7 | 0 1 0 | 6 0 0 | | 0 9 0 | 0 0 5 | 1 0 0 | ------------------------- | 5 0 0 | 8 4 0 | 9 0 0 | | 2 8 0 | 0 7 0 | 0 6 0 | | 0 0 0 | 6 0 9 | 2 1 0 | -------------------------
I am struck at the portion where i need to apply the 3 rules on the sudoku.... >__< i dont know how to implement it.. 1st rule.. the whole row no same value appears but ignore the 0 as 0 = no value. 2nd rule.. apply 1st rule on the coluumn 3rd rule each 3x3 box.. there is no repeated value
Take it step by step and check the program works as you go along. One way to check for duplicates is to have 9 flags, one for each number. Set all to zero. Loop over the first row (which you should be able to do easily enough), and at each non-zero digit check the relevant flag. If the flag is 0 then you haven't met that number before, so set the flag to 1. If the flag is 1 then you have a duplicate. Then modify that code so that it will loop over any row, and you can then put the code into another loop that selects the row for it to operate on. Then modify that code so that it will work over columns and check all columns are without duplicates. Then you may have enough understanding to work out how to modify the code to work over each 3x3 block, but don't worry about that step just yet; get the other steps working first.
May i know how should i initiate the 9 flag?? just temp1=0, temp2=0, etc? i still dont get how the flag work ><? Thanks a lot
The flag is just operating as an indicator so you can see whether or not it has been set. You can choose any datatype you want; it's just a 1 or 0. The flag "works" by being 1 if you've encountered the number already, and 0 if you haven't. So on the first time you encounter a number, you set it from 0 to 1. If you never encounter that number again then there's nothing to do. If you do encounter the number again then you will look at the flag and see that it has already been set, and then you know you have a duplicate. Yes, if you call the flag temp1, then you can initialise it with "int temp1=0;" However it may be easier to use an array, e.g. int flags[10], which you can initialise with a simple for loop.