Question

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 a console-based, yet convenient and intuitive user interface to get first two rows of the grid from the user. Use data validation to ensure user enters exactly nine integers without entering an invalid data, for example, non-numeric values or integers less than 1 or greater than 9.

4- You should use multi-dimensional array to represent the grid.

5- Once the grid is fully populated, display the grid in a 9 x 9 matrix format, followed by your decision of whether it forms a valid Sudoku solution or not.

6- As a test case, your code should prove that the following is a valid Sudoku solution.


2873-6459 14 62958 37 935478261 852134796 794652318 613789542 428963-75 379521684 5 618 4 7923

OUTPUT SHOULD BE LIKE THIS

5 3 4 6 78 91 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 485 6 9 6 1 5 3 7 2 8 4 2 87

2873-6459 14 62958 37 935478261 852134796 794652318 613789542 428963-75 379521684 5 618 4 7923
5 3 4 6 78 91 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 485 6 9 6 1 5 3 7 2 8 4 2 87 4 1 9 6 3 5 3 4 5 2 8 6 1 79 It is a valid solution Enter row 1 of your Sudoku grid Use Space/Tab/Return key to enter the next values and press 'Return' after entering nine integers. Remember, to make a valid row of Sudoku grid, all integers should be distinct and range from 1 to 9 3 4 5 67 8 7 1 1 Enter row 2 of your Sudoku grid Use Space/Tab/Return key to enter the next values and press Return' after entering nine integers. Remember, to make a valid row of Sudoku grid, all integers should be distinct and range from 1 to 9 2 1 2 3 4 5 6 7 8 9 -Sudoku Grid---- 3 45 6 78 71 1 1 2 3 4 5 6 7 89 6 9 85 2 4 1 3 7 8 6 9 4 1 7532 8 4 3 7 26 5 1 9 2 74 3 9 1 5 6 8 4 8 2 6 75 3 91 3 7 9 5 6 4 2 1 8 7 2 6 1 3 8 9 5 4 It is an invalid solution Process returned e (0xe) execution time 217.056 s Press any key to continue
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

//Header files

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

//Function to return 1 if sudoku grid is valid and 0 otherwise

int checkWin(int b[9][9])

{

int i, j, k, l, m, n;

//Invalid if any value is not from 1 to 9

for(i = 0; i < 9; i++)

for( j = 0; j < 9; j++)

if(b[i][j] < 1 || b[i][j] > 9)

return 0;

//Invalid if any value repeats in the row

for(i = 0; i < 9; i++)

for( j = 0; j < 9; j++)

for(k = 1; k < j; k++)

if(b[i][j] == b[i][k])

return 0;

//Invalid if any value repeats in the column

for(j = 0; j < 9; j++)

for(i = 0; i < 9; i++)

for(k = 1; k < i; k++)

if(b[i][j] == b[k][j])

return 0;

//Invalid if any value repeats in the block

for(i = 0; i < 9; i++)

for(j = 0; j < 9; j++)

{

m = (int)(i / 3) * 3;

n = (int)(j / 3) * 3;

for(k = m; k < m + 3; k++)

for(l = n; l < n + 3; l++)

if((i != k || j != l) && (b[i][j] == b[k][l]))

return 0;

}

//Otherwise, valid

return 1;

}

//Function to print the result accordingly

void printResult(int result)

{

if(result == 1)

printf("It is a valid solution.\n");

else

printf("It is an invalid solution.\n");

}

//Fucntion to display the grid

void displayGrid(int arr[9][9])

{

int i, j;

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

for(i = 0; i < 9; i++)

{

printf("\n");

for(j = 0; j < 9; j++)

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

}

printf("\n\n");

}

//main() begins

int main(void)

{

int i, j, k;

//Test case grid

int arr[9][9] = {5, 3, 4, 6, 7, 8, 9, 1, 2,

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

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

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

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

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

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

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

3, 4, 5, 2, 8, 6, 1, 7, 9};

displayGrid(arr);

printResult(checkWin(arr));

//Get 2 rows of input

for(i = 0; i < 2; i++)

{

printf("Enter row %d of your Sudoku grid\n", i + 1);

printf("Use Space/Tab/Return key to enter the next values\n");

printf("and press \'Return\' after entering nine integers.\n");

printf("Remember, to make a valid row of Sudoku grid,\n");

printf("all integers should be distinct and range from 1 to 9\n");

for(j = 0; j < 9; j++)

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

}

//Generate remaining rows

srand(time(0));

for(i = 2; i < 9; i++)

for(j = 0; j < 9; j++)

{

arr[i][j] = 1 + rand() % 9;

//Repeat the element if it is already present in that row

for(k = 0; k < j; k++)

if(arr[i][j] == arr[i][k])

j--;

}

displayGrid(arr);

printResult(checkWin(arr));

}

Output: (User input is in bold)

-------------Sudoku Grid-------------
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

It is a valid solution.
Enter row 1 of your Sudoku grid
Use Space/Tab/Return key to enter the next values
and press 'Return' after entering nine integers.
Remember, to make a valid row of Sudoku grid,
all integers should be distinct and range from 1 to 9
3 4 5 6 7 8 7 1 1
Enter row 2 of your Sudoku grid
Use Space/Tab/Return key to enter the next values
and press 'Return' after entering nine integers.
Remember, to make a valid row of Sudoku grid,
all integers should be distinct and range from 1 to 9
1 2 3 4 5 6 7 8 9

-------------Sudoku Grid-------------
3 4 5 6 7 8 7 1 1
1 2 3 4 5 6 7 8 9
2 6 5 8 3 1 4 9 7
4 3 8 5 7 2 6 9 1
1 9 2 4 5 7 6 8 3
7 5 9 4 8 6 2 3 1
8 5 2 3 4 1 7 9 6
6 5 3 1 4 8 2 7 9
7 3 1 8 5 6 4 2 9

It is an invalid solution.

Kindly give a thumbs up, if found useful. Comment for queries. :)

Add a comment
Know the answer?
Add Answer to:
C PROGRAMMING You must write a C program to check the validity of a Sudoku solution. Follow the link to know more about...
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
  • In this assignment, you must write a C program to check the validity of a Sudoku solution. You must at least do the foll...

    In this assignment, you must write a C program to check the validity of a Sudoku solution. 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 a random number generator. 2- Use appropriate logic to make sure the random number generator generates a distinct set of valid integers! 3- It should be a console-based, yet convenient and...

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

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • I'm making a sudoku solver to check if the sudoku grid created is legal. There are...

    I'm making a sudoku solver to check if the sudoku grid created is legal. There are supposed to be no repeated numbers in each row, column, and block. I got the code for the repeated numbers for row and column but confused on how to write the code for the blocks. Here is my code: 134 135 136 / COMPLETE THIS 137 // Returns true if this grid is legal. A grid is legal if no row, column, or //...

  • USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku...

    USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku game and show its result in detail. This is an application program of using 2-dimensional arrays or lists. Each array or list is a game board of 9 x 9. Your program must check the 9 x 9 game board, and report all the problems among 9 rows, 9 columns, and 9 squares. As you can see, you must pre-load the following four games...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • JAVA Sudoku.java This class does the work of solving the Sudoku puzzle, as well as containing...

    JAVA Sudoku.java This class does the work of solving the Sudoku puzzle, as well as containing the main method to provide a text-based user interface. Stores a Board object which it uses in solving. It should also track statistics about the solving process: the number of recursive calls made, and the number of "backups" that had to be done. public Sudoku( Scanner sc ) Constructor for the Sudoku class. Initializes the board and other instance variables. public boolean solve( Location...

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

  • C language Credit for work. Please design a multithreaded application in C with Pthreads - it...

    C language Credit for work. Please design a multithreaded application in C with Pthreads - it determines whether the solution to a Sudoku puzzle is valid. Validate two puzzles In your program, please hard-code the following two 9x9 grids (say in two variables puzzle1 and puzzle2), and determine if each solution is valid. While each grid should be validated by multiple parallel threads, you can validate puzzle1 and then puzzle2 in sequential order (as illustrated) by a single invocation of...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: 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