This program will partially check the validity of a given sudoku puzzle. The user will enter the puzzle as 9 rows of 9 numbers in the range of 1-9. If the same digit appears twice on a row or column then the puzzle is considered invalid and should be reported as such. If no duplicates are found then the program should report that the puzzle is partially correct.
For example:
This is Brent's Sudoku Puzzle Checker.
Please enter all 81 numbers corresponding to the 9 rows of 9 numbers each.
Remember that only numbers in the range 1-9 are allowed.
1 2 3 4 5 6 7 8 9
7 8 9 1 2 3 4 5 6
4 5 6 7 8 9 1 2 3
3 1 2 8 4 5 9 6 7
6 9 7 3 1 2 8 4 5
8 4 5 6 9 7 3 1 2
2 3 1 5 7 4 6 9 8
9 6 8 2 3 1 5 7 4
5 7 4 9 6 8 2 3 1
This puzzle is partially correct. If each 3x3 section does
not contain duplicates, then this puzzle would be correct.
#include <iostream>
#include <algorithm>
template<size_t rows, size_t columns>
class Soduko
{
public:
Soduko()
{
std::fill_n(&data[0][0], rows * columns, 0);
}
void set(size_t row, size_t col, int val)
{
// check if row and col and val are valid
data[row][col] = val;
}
int get(size_t row, size_t col) const
{
// check if row and col are valid
return data[row][col];
}
private:
int data[rows][columns];
};
int main()
{
Soduko<9,9> soduko;
}
#include <bits/stdc++.h>
usingnamespace std;
int main(){
cout << "This is Jakes's Sudoku Puzzle Checker." << endl;
cout << "Please enter all 81 numbers corresponding to the 9 rows of 9 numbers each." << endl;
cout << "Remember that only numbers in the range 1-9 are allowed." <<endl;
int sudoku[9][9];
int i, j, sum ,temp, flag = 1;
cout << "Enter a 9x9 sudoku: " << endl;
for(i = 0; i < 9; i++){
for(j = 0; j < 0; j++){
scanf("%d", &temp);
if(temp > 0 && temp < 10) // check for valid input for Sudoku
sudoku[i][j] = temp;
else{ // if any element is not invalid then successive breaks
flag = 0;
cout << "Invalid Input";
break;
}
}
if(flag == 0)
break;
}
if(flag){ // if every input is in valid range then go for validation of Sudoku
for(i = 0;i < 9; i++){
sum = 0;
for(j = 0; j < 9; j++)
sum = sum + sudoku[i][j];
if(sum != 45)
break; // if sum is not 45 in any row then go out of for loop
}
if(sum == 45){ // if sum is 45 in all rows then check for columns
for(i = 0; i < 9; i++){
sum = 0;
for(j = 0; j < 9; j++)
sum = sum + sudoku [j][i];
if(sum != 45)
break; // f sum is not 45 in any column then go out of for loop
}
if(sum == 45) // if sum is 45 in all columns too
cout << "Valid";
else
cout << "Invalid";
}
else
cout << "Invalid";
}
return 0;
}
This is Jake's Sudoku Puzzle Checker.
Please enter all 81 numbers corresponding to the 9 rows of 9 numbers each.
Remember that only numbers in the range 1-9 are allowed.
Enter a 9x9 sudoku:
Invalid
If gives me a straight invalid answer. How do I fix it?
So I changed some of the code to make it make more sense for me. Now there a errors to it. How would I fix them?
g++ "Sudoku Puzzle Checker.cpp" -o "Sudoku Puzzle Checker.exe"
Sudoku Puzzle Checker.cpp: In function 'int main()':
Sudoku Puzzle Checker.cpp:20:20: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
const int sudSize {9};
^
Sudoku Puzzle Checker.cpp:21:19: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
const int sudSum {sudSize * (sudSize + 1) / 2};
^
Sudoku Puzzle Checker.cpp:27:30: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int sudoku[sudSize][sudSize]{};
^
Sudoku Puzzle Checker.cpp:28:10: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
bool bad{};
^
Sudoku Puzzle Checker.cpp:28:11: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
bool bad{};
^
Sudoku Puzzle Checker.cpp:34:12: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int temp{};
^
Sudoku Puzzle Checker.cpp:34:13: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int temp{};
^
Sudoku Puzzle Checker.cpp:46:10: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int sum {};
^
Sudoku Puzzle Checker.cpp:46:11: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int sum {};
^