Question: Write a program that reads a 9x9 grid of digits ranging from 1 to 9 and determines whether or not the grid is a solution

Write a program that reads a 9x9 grid of digits ranging from 1 to 9 and determines whether or not the grid is a solution to a sudoku puzzle. Hint: use a 2D array. Hint: Define a separate function which accepts nine integers and returns whether or not they are unique. Then, repeatedly use this function. The following is the given puzzle and solution we are checking: 4 2 9 8 1 3 5 6 7 5 1 6 4 7 2 9 3 8 7 8 3 6 5 9 2 4 1 6 7 2 1 3 4 8 5 9 3 9 5 2 8 6 1 7 4 8 4 1 7 9 5 6 2 3 1 5 8 3 6 7 4 9 2 9 3 4 5 2 8 7 1 6 2 6 7 9 4 1 3 8 5 Here is the code so far. However it will not print out the puzzle nor says if the solution is valid. Not sure what is wrong here. //This program is going to put together a 9x9 sudoku puzzle with a given solution.
//The solution will be checked to see if it is valid.
#include 
using namespace std;
 
int check_unique(int x[])
{
int a[9]={0};
for(int i=0;i<9;i++)
{
if(a[x[i]-1] == 0)
a[x[i]-1] =1;
else
return 0;
}
return 1;
}
 
int main() {
 //Sudoku input
 int sudoku[9][9];
 cout<<"Enter the Sudoku grid: ";
 
 for(int i=0;i<9;i++)
 {
 for(int j=0;j<9;j++)
 cin>>sudoku[i][j];
 }
 
 int check=1,i,j,f;
 int x[9]={0};
 
 //Function to check sodoku to see if it is a correct solution.
 
 //Vertical grid check
 for(i=0;i<9;i++)
 {
 for(j=0;j<9;j++)
 {
 x[j] = sudoku[i][j];
 }
 f = check_unique(x);
 if(f==1)
 check = 0;
 }
 
 //Horizontal grid check
 
 for(i=0;i<9;i++)
 {
 for(j=0;j<9;j++)
 {
 x[j] = sudoku[j][i];
 }
 f = check_unique(x);
 if(f==1)
 check = 0;
 }
 
 //Checking box #1
 int k=0;
 for(i=0;i<3;i++)
 {
 for(j=0;j<3;j++)
 {
 x[k] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 //Checking box #2
 int k1=0;
 for(i=0;i<3;i++)
 {
 for(j=3;j<6;j++)
 {
 x[k1] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 
 //Checking box #3
 int k2=0;
 for(i=0;i<3;i++)
 {
 for(j=6;j<9;j++)
 {
 x[k2] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 //Checking box #4
 int k3=0;
 for(i=3;i<6;i++)
 {
 for(j=0;j<3;j++)
 {
 x[k3] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 //Checking box #5
 int k4=0;
 for(i=3;i<6;i++)
 {
 for(j=3;j<6;j++)
 {
 x[k4] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 //Checking box #6
 int k5=0;
 for(i=3;i<6;i++)
 {
 for(j=6;j<9;j++)
 {
 x[k5] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 //Checking box #7
 int k6=0;
 for(i=6;i<9;i++)
 {
 for(j=0;j<3;j++)
 {
 x[k6] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 //Checking box #8
 int k7=0;
 for(i=6;i<9;i++)
 {
 for(j=3;j<6;j++)
 {
 x[k7] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 //Checking box #9
 int k8=0;
 for(i=6;i<9;i++)
 {
 for(j=6;j<9;j++)
 {
 x[k8] = sudoku[i][j];
 k++;
 }
 }
 f = check_unique(x);
 if(f==1)
 check =0;
 
 //Final determination if solution is valid.
 if(check == 0)
 cout<<"Incorrect Solution"< 
 else
 cout<<"Correct Solution"< 
 return 0;
}
 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!