Question

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;


}

13 Save Sudoku board to a file Implement a function with the following prototype: int write_sudoku_board(const char file_name3 Check board validity Implement a function that checks whether a board is valid: int is valid_board(int board[9][9]) The fun

13 Save Sudoku board to a file Implement a function with the following prototype: int write_sudoku_board(const char file_name[], int board[91[91); This function should save the board to a file. The function should return 0 on success, -1 if any file I/O error occurred and -2 if an invalid board was provided. In the output file, write a line for each row, with no spaces, and use a dash (the '-' character) to represent blanks. For example, the Sudoku board on page 1 should be saved as follows: 6-32-8-5 4--1 7 23---7-6 4--1 57--492 3--64 7 1 31-758--6 Hint: If you have read the board from the same file (using the function from Step 12), make sure you close the file and re-open it before saving the board.
3 Check board validity Implement a function that checks whether a board is valid: int is valid_board(int board[9][9]) The function should return 0 if the board parameter violates any of the Sudoku rules and board is valid. You have to check four constraints: fhe Each cell holds a number between 1-9 (for the filled cells) or 0 (for the blanks). . No umber is repeated in any row. . No number is repeated in any column . No number is repeated in any box
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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...

    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;...

    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...

    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...

    * 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...

  • This is my code for my game called Reversi, I need to you to make the...

    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...

  • C PROGRAMMING You must write a C program to check the validity of a Sudoku solution. Follow the link to know more about...

    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...

  • Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending)....

    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...

    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...

    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...

  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT