Question

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: 3 Check board validity Implement a function that checks whether a board is valiod The function should return 0 if the board p

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;


}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*Kindly refer below resolved Code */

int main() {

    int a[10][10],i,j,n,k,sum,sum1,sum2,sum3,x,l;

    printf("enter the size N of N*N sudoku\n");            /* Enter the size of sudoku matrix.*/

    scanf("%d",&n);

    printf("enter the entries of sudoku row wise \n");     /* Going to read sudoku matrix (NxN).*/

    for (i=1;i<=n;i++) {

        for (j=1;j<=n;j++) {

            scanf("%d",&a[i][j]);

        }

        printf("\n");

    }

    printf("---------------------------------\n\n\n\n");

    printf("the matrix you entered is \n");

    for (i=1;i<=n;i++) {

        for (j=1;j<=n;j++) {

            printf("%d",a[i][j]);

            printf("|");

        }

        printf("\n");

    }

    for (i=1;i<=n;i++) {

        for (k=i;k==i;k++) {

            sum=0;

            for (j=1;j<=n;j++) {

                sum = sum + a[i][j];

            }

            if(sum!=45)

                x=1;

        }

    }

    for (j=1;j<=n;j++) {

        for(k=j;k==j;k++) {

            sum=0;

            for (i=1;i<=n;i++) {

                sum = sum+a[i][j];

            }

            if (sum!=45)

                x=1;

        }

    }

    for (k=1;k<=3;k++) {

        l = (1+(k-1)*n/3);

        for (i=l;i<=k*n/3;i++) {

            for(j=1;j<=3;j++) {

                sum1 = sum1+a[i][j];

            }

            for (j=4;j<=6;j++) {

                sum2 = sum2+a[i][j];

            }

            for (j=7;j<=9;j++) {

                sum3 = sum3+a[i][j];

            }

        }

        if (sum1!=45||sum2!=45||sum3!=45)

            x=1;

    }

    if (x==1)

        printf("sudoku not correct \n");

    else

        printf("correct sudoku");

    return 0;

}

/*Output*/

enter the size N of N*N Sudoku 3 enter the entries of sudoku row wise 2 3 3 2 2 3 the matrix you entered is 112131 312111 112

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

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

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

  • Need help in c++ programming to output the lines in my code: if word if found:...

    Need help in c++ programming to output the lines in my code: if word if found:            cout << "'" << word << "' was found in the grid" << endl;            cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • I need help making my array into a function that can called by the main function...

    I need help making my array into a function that can called by the main function using my program. This is C programming int prime (int x) { int i; i = 2; while (i < x) { if (x % i == 0) return 0; i++; } return 1; } int main (void){ int n; scanf ("%d", &n); if (n < 1) { printf ("Error: at least one data value must be provided.\n"); return 1; } int a[n]; int...

  • Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here }...

    Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here } int main() { int sudoku[9][9] = {{7, 3, 5, 6, 1, 4, 8, 9, 2}, {8, 4, 2, 9, 7, 3, 5, 6, 1}, {9, 6, 1, 2, 8, 5, 3, 7, 4}, {2, 8, 6, 3, 4, 9, 1, 5, 7}, {4, 1, 3, 8, 5, 7, 9, 2, 6}, {5, 7, 9, 1, 2, 6, 4, 3, 8}, {1, 5, 7, 4,...

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

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

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is 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