Background

In the last several years, a new logic puzzle called Sudoku has become quite popular throughout the world. In Sudoku, we start with a 9 x 9 grid of integers in which some of the cells have been filled in with digits between 1 and 9. Our job in the puzzle is to fill in each of the empty spaces with a digit between 1 and 9 so that each digit appears exactly once in each row, each column, and each of the smaller 3 x 3 squares. Each Sudoku puzzle is carefully constructed so that there is only one solution. For example, given the puzzle shown on the left of the following diagram, the unique solution is shown on the right: see image.

HW- Sudoku:

Currently you may not have learned the algorithmic strategies that you need to solve Sudoku puzzles. But you can easily

  • declare a data structure to represent a Sudoku puzzle,
  • write some functions to read Sudoku puzzle from a file,
  • check if a proposed solution follows the Sudoku rules against duplicating values in a row, column, or outlined 3 x 3 square (if needed, we will discuss Sudoku rules in the class).
  • also write a function that can list the possible values for each empty cell according to Sudoku rules.

So instead of asking you to write a program that can solve a Sudoku puzzle, you are asked to write a program that can do some of the simple things mentioned above. Specifically, your program must do the followings:

1.Declare a minimum size data structure to store the values of a Sudoku puzzle (e.g., 9x9 array of char)

2.From the command line, your program will get the file name that contains the data for a Sudoku puzzle or a Sudoku solution.

3.The given file would be formatted as follows: there is a number that indicates if this file contains a puzzle or a solution. If the that number is 1, then this file contains a Sudoku puzzle; if that number is 2, then the file contains a Sudoku solution to be checked.

4.The file than contains 9x9=81 values representing the values on Sudoku puzzle or solution.

a.In the case of a puzzle (the first number is 1), the remaining 9x9=81 numbers in the file will be between 0 and 9. Zero is for empty cells, 1-9 are for other cells.
b.In the case of a solution (the first number is 2), the remaining 9x9=81 numbers in the file will be between 1 and 9.

For example, here is a sample puzzle saved in puzzle1.txt 1

0 0 2 4 0 5 8 0 0
0 4 1 8 0 0 0 2 0
6 0 0 0 7 0 0 3 9
2 0 0 0 3 0 0 9 6
0 0 9 6 0 7 1 0 0
1 7 0 0 5 0 0 0 3
9 6 0 0 8 0 0 0 1
0 2 0 0 0 9 5 6 0
0 0 8 3 0 6 9 0 0

I post this file and other files on the class web page and BB Learn so you can download them and save them under the same directory where you will develop your hw1.c. You can create other files too. We will check your programs using other files as well...

Now we can run your program as follows:

elk03>hw1puzzle1.txt

5.If the given file contains a Sudoku puzzle (i.e., the first number in the file is 1), then your program should print possible values that can appear in each empty cell (the one that contains 0). In the case of the above puzzle data, the output would be as follows

[0][0]: 3, 7
[0][1]: 3, 9
[0][4]: 1, 6, 9
.....

6.If the given file contains a Sudoku solution (i.e., the first number in the file is 2), then your program should check if the solution is a valid one according to Sudoku rules. Accordingly prints Yes or No.

7.When implementing your program use modular programming as much as possible. Don't implement to many nested loops for each task! For example, you may develop some utility functions that will be helpful for performing the tasks in items 5 and 6 (try to avoid the same codes that are doing the same things in different parts of your program).

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.