This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!!
Modify my code if anything return statement seems misplaced or inaccurately called.
Here is my code:
int write_sudoku_board(const char file_name[ ], int board[9][9])
{
int number;
int i,j;
int a = 9;
FILE* fp = fopen("sudoku.txt", "w");
int count = 0;
for(i = 0; i < a; i++){
for(j = 0; j < 9; j++) {
if (board[i][j] == 0)
fprintf(fp, "-");
else {
fprintf(fp, "%d", board[i][j]);
}
}
fprintf(fp,"\n");
count++;
}
number = is_valid_board(board);
if(number == 1)
return 0;
else if(count != a-1)
return -1;
else
return -2;
}
int is_valid_board(int board[9][9]) {
int array[10];
int array_box[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int i, j, k, x, y; //rows=i, columns =j
for (i=0; i<9; i++) {
//Reset test array
for (x = 0; x < 10; x++) {
array[x] = 0;
}
//Checking for columns
for (j=0; j<9; j++) {
k = board[j][i];
if (board[j][i] >= 0 && board[j][i]<= 9 && k != 0 && array[k]) { //ensuring values are between 1-9
print_sudoku_board(board);
return 0;
}
array[k]++;
}
//Reset test array
for (x = 0; x < 10; x++) {
array[x] = 0;
}
//Checking for columns
for (j=0; j<9; j++) {
k = board[i][j];
if (board[i][j] >= 0 && board[j][i]<= 9 && k != 0 && array[k]) { //ensuring values are between 1-9
print_sudoku_board(board);
return 0;
}
array[k]++;
}
}
for (i=0; i<3; i++) {
for (j=0; j<3; j++) {
//Reseting array for box
for (x = 0; x < 10; x++) {
array_box[x] = 0;
}
for (x = i * 3; x < i * 3 + 3; x++) {
for (y = j*3; y < j * 3 + 3; y++) {
k = board[x][y];
if (k != 0 && array_box[k]) {
print_sudoku_board(board);
return 0;
}
array_box[k]++;
}
}
}
}
return 1;
}

![3 Check board validity Implement a function that checks whether a board is valid: int is valid_board(int board[9][9]) The fun](http://img.homeworklib.com/images/1c6adb19-dd43-4c7f-94f2-17c99385a2d8.png?x-oss-process=image/resize,w_560)
Your code seems fine to me . You have put all return statements at correct positions like if board is valid you are returning is 0 and if invqlid you are returning -2 . Also if there is any file input error then count wont be a-1 hence you are returning -1 which is according to conditions given in the question.
One thing i want to mention that you should close your file after all opeartions using fclose function as it is a good practice to close it.
Also you are passing correct errors in is_valid_board function by passing 0 for invalid board and 1 for valid board.
This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify m...
Help due in a few hours I need to clean this up can you help me
clean up this code to accomplish the same task but with fewer
lines/ more elegantly.
Thank you!
My code for the function is below:
int is_valid_board(int board[9][9]) {
int array[10];
int array_box[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int i, j, k, x, y; //rows=i, columns =j
for (i=0; i<9; i++) {
//Reset test array
for (x = 0; x...
How do I set up my read
function?
here's my code so far:
int read_sudoku_board(const char file_name[], int board[9][9])
{
FILE * fp = fopen("sudoku.txt", "r");
int a,i,j,c;
int count = 0;
for(i = 0; i < a; i++){
for(j = 0;j < 9;j++){
c = fgetc(fp);
if(c == '-'){
board[i][j] = 0;
}
else if(c >= 1 && c <= 9)
printf(" %d");
else
return -2;
}
count++;
}
if(count != a-1)
return -1;
else
return 0;
}12 Read...
Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions: 1. First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....
* Your goal in this exercise is to practice recursion and * to see how a properly written recursive solution can * take care of fairly complicated tasks with a few lines * of (well thought out) code. * * We will be solving Sudoku puzzles. In case you have never * solved or seen a Sudoku, you can learn more about them * here: * * https://en.wikipedia.org/wiki/Sudoku * * Your task if to write a function that takes an...
C PROGRAMMING
You must write a C program to check the validity of a Sudoku
solution. Follow the link to know more about Sudoku:
https://www.bigfishgames.com/blog/how-to-solve-sudoku-puzzles-quickly-andreliably/
You must at least do the following:
1- Ask the user to provide a minimum of first two rows of the
Sudoku grid. For the rest of the entries, you should use random
number generator.
2- Use appropriate logic to make sure random number generator
generates distinct set of valid integers!
3- It should be...
This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...
Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt: "Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data......
I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...
Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...