| | |
C++ function to check validity of sudoku
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
Hello all!
New here at daniweb.
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle.
this is what I have come up with so far:
Any ideas?
New here at daniweb.
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle.
this is what I have come up with so far:
C++ Syntax (Toggle Plain Text)
/** Check whether grid[i][j] is valid in the grid */ bool isValid(int grid[] [9]) { int i, j; bool status; status = true; for (int column = 0; column < 9; column++) if (column != j && grid[i] [column] == grid[i] [j]) status = false; for (int row = 0; row < 9; row++) if (row != i && grid[row] [j] == grid[i] [j]) status = false; for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++) for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++) if (row != i && col != j && grid[row] [col] == grid[i] [j]) status = false; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) if (grid[i][j] != 0) status = false; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) if ((grid[i][j] < 0) || (grid[i][j] > 9)) status = false; return status; }
Any ideas?
•
•
•
•
Hello all!
New here at daniweb.
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle.
this is what I have come up with so far:
...
Any ideas?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
Well of course...
This is just a function of the program.
My main question is how to check the 9x9 array to determine if it is a valid puzzle.
Here is the program:
This is just a function of the program.
My main question is how to check the 9x9 array to determine if it is a valid puzzle.
Here is the program:
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> #include <iomanip> using namespace std; void readAPuzzle(int grid[] [9]); bool search(int grid[] [9]); void printGrid(int grid[] [9]); bool isValid(int grid[] [9]); int main() { // Read a Sudoku puzzle int grid[9] [9]; readAPuzzle(grid); printGrid(grid); if (isValid(grid)) cout << "Is a sudoku puzzle. " << endl; else cout << "Is not a sudoku puzzle. " << endl; return 0; } //Reads Puzzle into 9x9 array ( modify to read from file ) void readAPuzzle(int grid[] [9]) { ifstream in_f; in_f.open("ma1.dat"); for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) in_f >> grid[i] [j]; } //Prints out the puzzle read from file (keep) void printGrid(int grid[] [9]) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) cout << grid[i] [j] << " "; cout << endl; } } /** Check whether grid[i][j] is valid in the grid */ bool isValid(int grid[] [9]) { int i, j; bool status; status = true; for (int column = 0; column < 9; column++) if (column != j && grid[i] [column] == grid[i] [j]) status = false; for (int row = 0; row < 9; row++) if (row != i && grid[row] [j] == grid[i] [j]) status = false; for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++) for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++) if (row != i && col != j && grid[row] [col] == grid[i] [j]) status = false; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) if (grid[i][j] != 0) status = false; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) if ((grid[i][j] < 0) || (grid[i][j] > 9)) status = false; return status; }
•
•
•
•
Well of course...
This is just a function of the program.
My main question is how to check the 9x9 array to determine if it is a valid puzzle.
It looks to me like you are already checking the 9x9 array in
isValid() so I don't understand exactly what you need help with. Please read the post Read Me: Read This Before Posting so you can ask exactly what you need to know in a way we can help you. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
I said: "I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle."
I figured that the code I posted could be fixed some how. My apologies for not submitting a proper question.
The problem I have run into is that the program does not know the difference from a valid and invalid puzzle.
I have several solved puzzles that I am in inputting, and several that are just random numbers. The isValid() function is supposed to check for this very occurrence. However it is not.
I figured that the code I posted could be fixed some how. My apologies for not submitting a proper question.
The problem I have run into is that the program does not know the difference from a valid and invalid puzzle.
I have several solved puzzles that I am in inputting, and several that are just random numbers. The isValid() function is supposed to check for this very occurrence. However it is not.
•
•
•
•
I said: "I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle."
•
•
•
•
I figured that the code I posted could be fixed some how. My apologies for not submitting a proper question.
•
•
•
•
The problem I have run into is that the program does not know the difference from a valid and invalid puzzle.
•
•
•
•
I have several solved puzzles that I am in inputting, and several that are just random numbers. The isValid() function is supposed to check for this very occurrence. However it is not.
In other words, read the link, ask a proper question.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
If I knew what was going wrong I wouldn't have asked the question or made the post.
My function is supposed to check whether or not a given input of a 9x9 matrix is a valid and solvable puzzle; that is, whether or not each row, column, and region in the matrix contains each value from 1 to 9 exactly once.
I have attempted to write the code that will do this, however for some reason my code is not working as it should. It is not checking whether or not a given input of a 9x9 matrix is a valid and solvable puzzle; that is, whether or not each row, column, and region in the matrix contains each value from 1 to 9 exactly once.
My function is supposed to check whether or not a given input of a 9x9 matrix is a valid and solvable puzzle; that is, whether or not each row, column, and region in the matrix contains each value from 1 to 9 exactly once.
I have attempted to write the code that will do this, however for some reason my code is not working as it should. It is not checking whether or not a given input of a 9x9 matrix is a valid and solvable puzzle; that is, whether or not each row, column, and region in the matrix contains each value from 1 to 9 exactly once.
Fine. If you can't tell us anything, run this version of your program. Maybe then it can give you a clue how to figure out what is happening.
Look at the output and you should be able to tell which loop is working improperly.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> #include <iomanip> using namespace std; void readAPuzzle(int grid[] [9]); bool search(int grid[] [9]); void printGrid(int grid[] [9]); bool isValid(int grid[] [9]); int main() { // Read a Sudoku puzzle int grid[9] [9]; readAPuzzle(grid); printGrid(grid); if (isValid(grid)) cout << "Is a sudoku puzzle. " << endl; else cout << "Is not a sudoku puzzle. " << endl; return 0; } //Reads Puzzle into 9x9 array ( modify to read from file ) void readAPuzzle(int grid[] [9]) { ifstream in_f; in_f.open("ma1.dat"); for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) in_f >> grid[i] [j]; } //Prints out the puzzle read from file (keep) void printGrid(int grid[] [9]) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) cout << grid[i] [j] << " "; cout << endl; } } /** Check whether grid[i][j] is valid in the grid */ bool isValid(int grid[] [9]) { int i, j; bool status; status = true; for (int column = 0; column < 9; column++) if (column != j && grid[i] [column] == grid[i] [j]) status = false; cout << "1st test: " << status << endl; for (int row = 0; row < 9; row++) if (row != i && grid[row] [j] == grid[i] [j]) status = false; cout << "2nd test: " << status << endl; for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++) for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++) if (row != i && col != j && grid[row] [col] == grid[i] [j]) status = false; cout << "3rd test: " << status << endl; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) if (grid[i][j] != 0) status = false; cout << "4th test: " << status << endl; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) if ((grid[i][j] < 0) || (grid[i][j] > 9)) status = false; cout << "5th test: " << status << endl; return status; }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
It appears that the line:
is incorrect.
How can I fix it?
C++ Syntax (Toggle Plain Text)
if (column != j && grid[i] [column] == grid[i] [j])
is incorrect.
How can I fix it?
![]() |
Similar Threads
- error C2064: term does not evaluate to a function taking 1 arguments (C++)
- Program Crashing PT2 (C++)
- Deleting characters function ( with just one parameter) (C)
- copy constructor and 2 args constructor help (C)
- Check Please! (C++)
- famous gets() function (C)
- Uploading images on a server (ASP)
- Need Help with a function... (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Check for valid date, fine if no date entered
- Next Thread: Help With Recursive function coding in C++
Views: 4022 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






