Question

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

4- You should use the 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.

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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<unistd.h>

void error(char *s,int i,int j)
{
printf("\nThe generated sudoku is not valid");
printf("\n at %s. Row:%d,Column:%d",s,i+1,j+1);
exit(0);
}

int main() {
time_t t;
   int arr[9][9],i,j,count,flag,si,sj;
   for(i=0;i<2;i++){
   for(j=0;j<9;j++){
   printf("Enter a number between 1 and 9 for a[%d][%d]\n",i,j);
   scanf("%d",&arr[i][j]);
   if(!(arr[i][j]>0&&arr[i][j]<10))
   {
   printf("Invalid input\n");
   j--;
   }
   }
   }
   srand((unsigned) time(&t));
   for(i=2;i<9;i++){
   count=0;
   while(count<9){
   int randNum=rand()%9+1;
   int found=0;
   for(j=0;j<count;j++){
   if(arr[i][j]==randNum){
   found=1;
   break;
   }
   }
   if(found==0){
   arr[i][j]=randNum;
   count++;
   }
   }
   }
   printf("Final two dimensional array:\n");
   for(i=0;i<9;i++){
   for(j=0;j<9;j++){
   printf("%d\t",arr[i][j]);
   }
   printf("\n");
   }
   /*checking rows
we check each cell in each row.
We start with a flag 0x0000.
if 1 is found zeroth bit of flag is set.
if 2 is found, first bit is set and so on.
If all digits 1 to 9 are present, flag's value will be 0x01FF.
If flag is 0x01FF after traversing a row,
the row has all numbers 1 to 9.
So, it is correct.
If the flag is not 0x01FF after traversing a row,
the row is incorrectly filled.
Then we call error() function
*/
for(i=0;i<9;i++)
{
flag=0x0000;
for(j=0;j<9;j++)
flag|=1<<(arr[i][j]-1);
if(flag!=0x01FF)
error("row",i,j-1);
}

/*checking columns
Just like row checking.
The flag is for a column.
*/
for(j=0;j<9;j++)
{
flag=0x0000;
for(i=0;i<9;i++)
flag|=1<<(arr[i][j]-1);
if(flag!=0x01FF)
error("col",i-1,j);
}
/*checking Squares (3x3)
Just like row checking.
The flag is for a square.
*/
for(si=0;si<3;si++)
{
for(sj=0;sj<3;sj++)
{
flag=0x0000;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
flag|=1<<(arr[si*3+i][sj*3+j]-1);
}
if(flag!=0x01FF)
error("square",si*3+i-1,sj*3+j-1);
}
}
printf("\nThe generated sudoku is valid");
   return 0;
}

Input Given:

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

Output :

Enter a number between 1 and 9 for a[0][0]
Enter a number between 1 and 9 for a[0][1]
Enter a number between 1 and 9 for a[0][2]
Enter a number between 1 and 9 for a[0][3]
Enter a number between 1 and 9 for a[0][4]
Enter a number between 1 and 9 for a[0][5]
Enter a number between 1 and 9 for a[0][6]
Enter a number between 1 and 9 for a[0][7]
Enter a number between 1 and 9 for a[0][8]
Enter a number between 1 and 9 for a[1][0]
Enter a number between 1 and 9 for a[1][1]
Enter a number between 1 and 9 for a[1][2]
Enter a number between 1 and 9 for a[1][3]
Invalid input
Enter a number between 1 and 9 for a[1][3]
Enter a number between 1 and 9 for a[1][4]
Enter a number between 1 and 9 for a[1][5]
Enter a number between 1 and 9 for a[1][6]
Enter a number between 1 and 9 for a[1][7]
Enter a number between 1 and 9 for a[1][8]
Final two dimensional array:
2   3   4   1   7   6   5   9   8  
9   7   8   1   5   2   4   6   3  
4   5   9   3   7   6   2   1   8  
5   2   8   9   6   3   7   1   4  
1   7   5   2   6   8   3   9   4  
4   7   1   2   8   9   6   3   5  
3   8   7   9   1   2   5   6   4  
8   4   2   3   7   1   9   5   6  
8   9   4   2   7   5   1   3   6  

The generated sudoku is not valid
at col. Row:9,Column:1

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

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

  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • Language: C++ PLEASE INCLUDE SCREENSHOT OF OUTPUT In this assignment, you will consider the problem of organizing a collection of computer user-ids and passwords. Each time a user logs in to the syste...

    Language: C++ PLEASE INCLUDE SCREENSHOT OF OUTPUT In this assignment, you will consider the problem of organizing a collection of computer user-ids and passwords. Each time a user logs in to the system by entering his or her user-id and a secret password, the system must check the validity of this user-id and password to verify that this is a legitimate user. Because this validation must be done many times each day, it is necessary to structure this information in...

  • with C++ You will create a program that uses a Critter class to move around a...

    with C++ You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • this is for c++ please and thank you! 19. Check Writer Write a program that displays...

    this is for c++ please and thank you! 19. Check Writer Write a program that displays a simulated paycheck. The program should ask the user to enter the date, the payee's name, and the amount of the check (up to $10,000). It should then display a simulated check with the dollar amount spelled out, as shown here: Date: 11/24/2014 Pay to the Order of: John Phillips $1920.85 One thousand nine hundred twenty and 85 cents Be sure to format the...

  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

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