**I provided template for this problem and need to finish the blank functions li
ID: 3749358 • Letter: #
Question
**I provided template for this problem and need to finish the blank functions listed below. Please just copy the following code and paste it to program and finish it. Thanks.**
**I provided template for this problem and need to finish the blank functions listed below. Please just copy the following code and paste it to program and finish it. Thanks.**
//These are the prototypes for the functions you will code!!!
void analyzePuzzle(char puzzValues[][9], char type, char check[]);
void checkRow(char puzzValues[][9], char check[], int row, int column);
void checkColumn(char puzzValues[][9], char check[], int row, int column);
void checkSquare(char puzzValues[][9], char check[], int row, int column);
void printPossibleNums(char puzzValues[][9], char check[], int row, int column);
int checkSolution(char puzzValues[][9], char check[]);
#include <stdio.h>
#include <stdlib.h>
void inputPuzzle(char puzzValues[][9], char *type, char *argv[]);
void printPuzzle(char puzzValues[][9], char type);
//These are the prototypes for the functions you will code!!!
void analyzePuzzle(char puzzValues[][9], char type, char check[]);
void checkRow(char puzzValues[][9], char check[], int row, int column);
void checkColumn(char puzzValues[][9], char check[], int row, int column);
void checkSquare(char puzzValues[][9], char check[], int row, int column);
void printPossibleNums(char puzzValues[][9], char check[], int row, int column);
int checkSolution(char puzzValues[][9], char check[]);
int main(int argc, char *argv[])
{
int solution;
char puzzValues[9][9];
char type;
char check[10];
//Check to make sure a puzzle file was given
if (argc != 2) {
printf("Command line error ");
exit(-1);
}
inputPuzzle(puzzValues, &type, argv);
printPuzzle(puzzValues, type);
if (type == 1)
analyzePuzzle(puzzValues, type, check);
else if (type == 2)
{
solution = checkSolution(puzzValues, check);
if (solution == 0)
printf("Yes ");
else if (solution == 1)
printf("No ");
else
printf("Finish your code! ");
}
return 0;
}
void inputPuzzle(char puzzValues[][9], char *type, char *argv[])
{
int userChoice, tmp, i, j;
int lBound, uBound = 9;
FILE *puzzleInput = fopen(argv[1], "r");
if (puzzleInput == NULL) {
printf("Error opening the file: %s", argv[1]);
exit(-1);
}
//Read the puzzle type
fscanf(puzzleInput, "%d ", &userChoice);
if (userChoice != 1 && userChoice !=2) {
printf("Invalid puzzle type: %d ", userChoice);
exit(-1);
}
*type = userChoice;
if (userChoice == 1)
lBound = 0;
else
lBound = 1;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if (fscanf(puzzleInput, "%d ", &tmp) == 1)
{
if (tmp < lBound || tmp > uBound) {
printf("Input value outside of bounds: %d", tmp);
exit(-1);
}
else
puzzValues[i][j] = tmp;
}
else {
printf("Error reading file ");
exit(-1);
}
}
}
fclose(puzzleInput);
}
void printPuzzle(char puzzValues[][9], char type)
{
int i, j;
printf("%d ", type);
for (i=0; i<9; i++)
{
for (j=0; j<9; j++)
printf("%d ", puzzValues[i][j]);
printf(" ");
}
}
/******************** analyzePuzzle **************************************
void analyzePuzzle(char puzzValues[][9], char type, char check[])
Purpose:
Analyzes an unfinished puzzle to determine possible values for each
empty square.
Parameters:
I char puzzValues[][]
I char type
I char check[]
Returns:
n/a
Notes:
Nested for loops are used to traverse the 2d array. For each sudoku value
at puzzValues[i][j], its row, column, and small square are checked. The
possible values are then printed.
**************************************************************************/
void analyzePuzzle(char puzzValues[][9], char type, char check[])
{
}
/******************** checkRow **************************************
void checkRow(char puzzValues[][9], char check[], int row, int column)
Purpose:
Checks the other elements in the row of the square being checked.
Parameters:
I char puzzValues[][]
I char check[]
I int row
I int column
Returns:
Increments values in check[] based on values found in the row.
Notes:
int row and int column are the coordinates of the square currently
being checked.
**************************************************************************/
void checkRow(char puzzValues[][9], char check[], int row, int column)
{
}
/******************** checkColumn **************************************
void checkColumn(char puzzValues[][9], char check[], int row, int column)
Purpose:
Checks the other elements in the column of the square being checked.
Parameters:
I char puzzValues[][]
I char check
I - int row
I - int column
Returns:
Increments values in check[] based on values found in the row.
Notes:
int row and int column are the coordinates of the square currently
being checked.
**************************************************************************/
void checkColumn(char puzzValues[][9], char check[], int row, int column)
{
}
/******************** checkSquare **************************************
void checkSquare(char puzzValues[][9], char check[], int row, int column)
Purpose:
Checks the other elements in the small square of the square being checked.
Parameters:
I char puzzValues[][]
I char check[]
I - int row
I - int column
Returns:
Increments values in check[] based on values found in the small square.
Notes:
int row and int column are the coordinates of the square currently
being checked. Integer division is used to identify the 3x3 square
containing [i][j]. Nested for loops are used to move through the small
square, incrementing check[].
**************************************************************************/
void checkSquare(char puzzValues[][9], char check[], int row, int column)
{
}
/******************** printPossibleNums **************************************
void printPossibleNums(char puzzValues[][9], char check[], int row, int column)
Purpose:
Prints the possible solutions to an empty square in a sudoku puzzle.
Parameters:
I char puzzValues[][]
I char check[]
I - int row
I - int column
Returns:
n/a
Notes:
A nested for loop is used to traverse the check[] array. If the
value at check[i] is zero, it means that i is a possible solution
for that square.
**************************************************************************/
void printPossibleNums(char puzzValues[][9], char check[], int row, int column)
{
}
/******************** checkSolution **************************************
int checkSolution(char puzzValues[][9], char check[])
Purpose:
Checks a sudoku solution using sudoku rules.
Parameters:
I char puzzValues[][]
I char check[]
Returns:
O - int 0 = good solution, 1 = wrong solution
Notes:
The check[] array is used to identify if a solution is correct or not.
**************************************************************************/
int checkSolution(char puzzValues[][9], char check[])
{
return -1;
}
Instead of asking you to write a program that can solve a Sudoku puzzle, we ask you to write a program that can do some of the simple things mentioned above. Some code is already provided for the first two tasks. You will focus on the last two tasks and implement the necessary functions HW- Sudoku (detailed description and necessary steps): The provided code hw01.c is already doing the followings: 1. Declare a minimum size data structure to store the values of a Sudoku puzzle (e.g, 9x9 array of char because we will be storing numbers between 0 and 9) Read Sudoku puzzle or Sudoku solution from a file whose name is given as a command line argument. .The given file would be formatted as follows: there is a number that indicates if 2. 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. 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 9Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void inputPuzzle(char puzzValues[][9], char *type, char *argv[]);
void printPuzzle(char puzzValues[][9], char type);
//These are the prototypes for the functions you will code!!!
void analyzePuzzle(char puzzValues[][9], char type, char check[]);
void checkRow(char puzzValues[][9], char check[], int row, int column);
void checkColumn(char puzzValues[][9], char check[], int row, int column);
void checkSquare(char puzzValues[][9], char check[], int row, int column);
void printPossibleNums(char puzzValues[][9], char check[], int row, int column);
int checkSolution(char puzzValues[][9], char check[]);
int main(int argc, char *argv[])
{
int solution;
char puzzValues[9][9];
char type;
char check[10];
int i ;
// setting check[i] equal to zero.
for ( i = 0 ; i <= 9; i ++ ){
check[i] = 0;
}
//Check to make sure a puzzle file was given
if (argc != 2) {
printf("Command line error ");
exit(-1);
}
inputPuzzle(puzzValues, &type, argv);
printPuzzle(puzzValues, type);
if (type == 1)
analyzePuzzle(puzzValues, type, check);
else if (type == 2)
{
solution = checkSolution(puzzValues, check);
if (solution == 0)
printf("Yes ");
else if (solution == 1)
printf("No ");
else
printf("Finish your code! ");
}
return 0;
}
/******************** inputPuzzle **************************************
void inputPuzzle(char puzzValues[][9], char *type, char *argv[])
Purpose:
Reads a sudoku puzzle in from a file. Stores the values.
Parameters:
I char puzzValues[][]
I char* type
I char* argv[]
Returns:
Returns the type of puzzle being entered and the sudoku numbers using pointers.
Notes:
The puzzle file is opened and input is error checked. The lower bound
is set by the puzzle type. Puzzles to be checked cannot have 0s. Puzzles
to find possible solutions use 0s to indicate a blank space. Nested for
loops are used to traverse the 2d array and input values.
**************************************************************************/
void inputPuzzle(char puzzValues[][9], char *type, char *argv[])
{
int userChoice, tmp, i, j;
int lBound, uBound = 9;
FILE *puzzleInput = fopen(argv[1], "r"); // reading in puzzlefile
if (puzzleInput == NULL) {
printf("Error opening the file: %s", argv[1]);
exit(-1);
}
//Read the puzzle type
fscanf(puzzleInput, "%d ", &userChoice);
if (userChoice != 1 && userChoice !=2) {
printf("Invalid puzzle type: %d ", userChoice);
exit(-1);
}
*type = userChoice;
if (userChoice == 1)
lBound = 0;
else
lBound = 1;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if (fscanf(puzzleInput, "%d ", &tmp) == 1)
{
if (tmp < lBound || tmp > uBound) {
printf("Input value outside of bounds: %d", tmp);
exit(-1);
}
else
// storing puzzlevalues[i][j] to tmp
puzzValues[i][j] = tmp;
}
else {
printf("Error reading file ");
exit(-1);
}
}
}
fclose(puzzleInput);
}
/******************** printPuzzle **************************************
void printPuzzle(char puzzValues[][9], char type)
Purpose:
Prints out the two dimensional array as a sudoku table.
Parameters:
I char puzzValues[][]
I char type
Returns:
n/a
Notes:
Nested for loops are used to traverse the two dimensional array.
**************************************************************************/
// this function printout the puzzleValues
void printPuzzle(char puzzValues[][9], char type)
{
int i, j;
// running a nested to printout the puzzValues[i][j];
printf("%d ", type);
// nested loop to traverse 2D array
for (i=0; i<9; i++)
{
for (j=0; j<9; j++)
printf("%d ", puzzValues[i][j]);
printf(" ");
}
}
/******************** analyzePuzzle **************************************
void analyzePuzzle(char puzzValues[][9], char type, char check[])
Purpose:
Analyzes an unfinished puzzle to determine possible values for each
empty square.
Parameters:
I char puzzValues[][]
I char type
I char check[]
Returns:
n/a
Notes:
Nested for loops are used to traverse the 2d array. For each sudoku value
at puzzValues[i][j], its row, column, and small square are checked. The
possible values are then printed.
**************************************************************************/
void analyzePuzzle(char puzzValues[][9], char type, char check[])
{
int i , j, g;
for ( i = 0; i <= 8 ; i ++){
for ( j = 0; j <= 8 ; j ++){
//checking the row
checkRow ( puzzValues, check, i, j);
//checking the colunm
checkColumn ( puzzValues, check, i, j);
//this will check the square 3 x 3, make sure there are no duplicates
checkSquare ( puzzValues, check, i, j);
// then all possible values in 9 positions.
printPossibleNums( puzzValues, check, i, j);
for ( g = 0; g <= 8; g ++ ){
check[g] = 0;
}
}
}
}
/******************** checkRow **************************************
void checkRow(char puzzValues[][9], char check[], int row, int column)
Purpose:
Checks the other elements in the row of the square being checked.
Parameters:
I char puzzValues[][]
I char check[]
I int row
I int column
Returns:
Increments values in check[] based on values found in the row.
Notes:
int row and int column are the coordinates of the square currently
being checked.
**************************************************************************/
void checkRow(char puzzValues[][9], char check[], int row, int column)
{
// char checkrow1[9];
int i , j, sum, count;
int n = 9;
// running to the row
for ( i = 0; i <= 9 ; i++ ){
// i will be colunms
check [puzzValues[row][i]] = 1;
}
for ( i = 0; i <= 9; i++){
// printf (" %d ", check[i]);
}
}
/******************** checkColumn **************************************
void checkColumn(char puzzValues[][9], char check[], int row, int column)
Purpose:
Checks the other elements in the column of the square being checked.
Parameters:
I char puzzValues[][]
I char check
I - int row
I - int column
Returns:
Increments values in check[] based on values found in the row.
Notes:
int row and int column are the coordinates of the square currently
being checked.
**************************************************************************/
void checkColumn(char puzzValues[][9], char check[], int row, int column)
{
int i;
// check each row
for ( i = 0; i <= 9 ; i++ ){
check[puzzValues[i][column]] = 1;
}
}
/******************** checkSquare **************************************
void checkSquare(char puzzValues[][9], char check[], int row, int column)
Purpose:
Checks the other elements in the small square of the square being checked.
Parameters:
I char puzzValues[][]
I char check[]
I - int row
I - int column
Returns:
Increments values in check[] based on values found in the small square.
Notes:
int row and int column are the coordinates of the square currently
being checked. Integer division is used to identify the 3x3 square
containing [i][j]. Nested for loops are used to move through the small
square, incrementing check[].
**************************************************************************/
void checkSquare(char puzzValues[][9], char check[], int row, int column)
{
// checking the starting point at middle point of each box 3 x 3
// check the middle point starting at first box on top left
if ( row >= 0 && row <=2 ){
if ( column >= 0 && column <= 2){
row = 1;
column = 1;
}
if ( column >= 3 && column <= 5 ){
row =1;
column =4;
}
if ( column >= 6 && column <= 8){
row =1;
column = 7;
}
}
// check this middle row
if ( row >= 3 && row <= 5 ){
if ( column >= 0 && column <= 2){
row = 4;
column = 1;
}
if ( column >= 3 && column <= 5 ){
row =4;
column =4;
}
if ( column >= 6 && column <= 8){
row =4;
column = 7;
}
}
if ( row >= 6 && row <= 8 ){
if ( column >= 0 && column <= 2){
row = 7;
column = 1;
}
if ( column >= 3 && column <= 5 ){
row =7;
column =4;
}
if ( column >= 6 && column <= 8){
row = 7;
column = 7;
}
}
// check all possible answer that we can get between 1 -9
check[puzzValues[row -1][column -1]] = 1;
check[puzzValues[row -1][column]] = 1;
check[puzzValues[row -1][column + 1]] = 1;
check[puzzValues[row][column -1]] = 1;
check[puzzValues[row] [column]] = 1;
check[puzzValues[row][column + 1]] = 1;
check[puzzValues[row + 1][column - 1]] = 1;
check[puzzValues[row + 1][column ]] = 1;
check[puzzValues[row + 1][column + 1]] = 1;
}
/******************** printPossibleNums **************************************
void printPossibleNums(char puzzValues[][9], char check[], int row, int column)
Purpose:
Prints the possible solutions to an empty square in a sudoku puzzle.
Parameters:
I char puzzValues[][]
I char check[]
I - int row
I - int column
Returns:
n/a
Notes:
A nested for loop is used to traverse the check[] array. If the
value at check[i] is zero, it means that i is a possible solution
for that square.
**************************************************************************/
void printPossibleNums(char puzzValues[][9], char check[], int row, int column)
{
int i;
printf(" [%d] [%d]: ", row, column);
for ( i = 1; i <= 9; i ++){
if ( check[i] == 0){
printf(" %d, ", i );
}
}
printf(" " );
}
/******************** checkSolution **************************************
int checkSolution(char puzzValues[][9], char check[])
Purpose:
Checks a sudoku solution using sudoku rules.
Parameters:
I char puzzValues[][]
I char check[]
Returns:
O - int 0 = good solution, 1 = wrong solution
Notes:
The check[] array is used to identify if a solution is correct or not.
**************************************************************************/
int checkSolution(char puzzValues[][9], char check[])
{
int i , j, g, k;
int line = 1;
for ( i = 0; i <= 8 ; i ++){
for ( j = 0; j <= 8 ; j ++){
checkRow ( puzzValues, check, i, j);
checkColumn ( puzzValues, check, i, j);
checkSquare ( puzzValues, check, i, j);
for ( k = 1; k <= 9; k ++ ){
if ( check [k] == 0){
// wrong solution
return 1;
}
}
for ( g = 0; g <= 9; g ++ ){
check[g] = 0;
}
}
}
// good solution
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.