C++ function to check validity of sudoku

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 9
Reputation: brlukosk is an unknown quantity at this point 
Solved Threads: 0
brlukosk brlukosk is offline Offline
Newbie Poster

C++ function to check validity of sudoku

 
0
  #1
Nov 20th, 2007
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:

  1. /** Check whether grid[i][j] is valid in the grid */
  2. bool isValid(int grid[] [9])
  3. {
  4. int i, j;
  5. bool status;
  6. status = true;
  7.  
  8. for (int column = 0; column < 9; column++)
  9. if (column != j && grid[i] [column] == grid[i] [j])
  10. status = false;
  11.  
  12. for (int row = 0; row < 9; row++)
  13. if (row != i && grid[row] [j] == grid[i] [j])
  14. status = false;
  15.  
  16. for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
  17. for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
  18. if (row != i && col != j && grid[row] [col] == grid[i] [j])
  19. status = false;
  20.  
  21. for (int i = 0; i < 9; i++)
  22. for (int j = 0; j < 9; j++)
  23. if (grid[i][j] != 0)
  24. status = false;
  25.  
  26. for (int i = 0; i < 9; i++)
  27. for (int j = 0; j < 9; j++)
  28. if ((grid[i][j] < 0) || (grid[i][j] > 9))
  29. status = false;
  30.  
  31.  
  32. return status;
  33.  
  34. }


Any ideas?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ function to check validity of sudoku

 
0
  #2
Nov 20th, 2007
Originally Posted by brlukosk View Post
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?
Maybe finish the program, I suppose...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: brlukosk is an unknown quantity at this point 
Solved Threads: 0
brlukosk brlukosk is offline Offline
Newbie Poster

Re: C++ function to check validity of sudoku

 
0
  #3
Nov 20th, 2007
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:

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7. void readAPuzzle(int grid[] [9]);
  8. bool search(int grid[] [9]);
  9. void printGrid(int grid[] [9]);
  10. bool isValid(int grid[] [9]);
  11.  
  12. int main()
  13. {
  14. // Read a Sudoku puzzle
  15. int grid[9] [9];
  16.  
  17. readAPuzzle(grid);
  18.  
  19. printGrid(grid);
  20.  
  21. if (isValid(grid))
  22. cout << "Is a sudoku puzzle. " << endl;
  23.  
  24. else
  25. cout << "Is not a sudoku puzzle. " << endl;
  26.  
  27. return 0;
  28. }
  29.  
  30. //Reads Puzzle into 9x9 array ( modify to read from file )
  31. void readAPuzzle(int grid[] [9])
  32. {
  33. ifstream in_f;
  34.  
  35. in_f.open("ma1.dat");
  36.  
  37. for (int i = 0; i < 9; i++)
  38. for (int j = 0; j < 9; j++)
  39. in_f >> grid[i] [j];
  40. }
  41.  
  42. //Prints out the puzzle read from file (keep)
  43. void printGrid(int grid[] [9])
  44. {
  45. for (int i = 0; i < 9; i++)
  46. {
  47. for (int j = 0; j < 9; j++)
  48. cout << grid[i] [j] << " ";
  49. cout << endl;
  50. }
  51. }
  52. /** Check whether grid[i][j] is valid in the grid */
  53. bool isValid(int grid[] [9])
  54. {
  55. int i, j;
  56. bool status;
  57. status = true;
  58.  
  59. for (int column = 0; column < 9; column++)
  60. if (column != j && grid[i] [column] == grid[i] [j])
  61. status = false;
  62.  
  63. for (int row = 0; row < 9; row++)
  64. if (row != i && grid[row] [j] == grid[i] [j])
  65. status = false;
  66.  
  67. for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
  68. for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
  69. if (row != i && col != j && grid[row] [col] == grid[i] [j])
  70. status = false;
  71.  
  72. for (int i = 0; i < 9; i++)
  73. for (int j = 0; j < 9; j++)
  74. if (grid[i][j] != 0)
  75. status = false;
  76.  
  77. for (int i = 0; i < 9; i++)
  78. for (int j = 0; j < 9; j++)
  79. if ((grid[i][j] < 0) || (grid[i][j] > 9))
  80. status = false;
  81.  
  82.  
  83. return status;
  84.  
  85. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ function to check validity of sudoku

 
0
  #4
Nov 20th, 2007
Originally Posted by brlukosk View Post
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.
Oh, I get it. That was not a question in your previous post.

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: brlukosk is an unknown quantity at this point 
Solved Threads: 0
brlukosk brlukosk is offline Offline
Newbie Poster

Re: C++ function to check validity of sudoku

 
0
  #5
Nov 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ function to check validity of sudoku

 
0
  #6
Nov 21st, 2007
Originally Posted by brlukosk View Post
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."
That's not a question. That is a statement. No one can tell if there is a problem or not.

Originally Posted by brlukosk View Post
I figured that the code I posted could be fixed some how. My apologies for not submitting a proper question.
It probably can. But you never mentioned what the problem is.

Originally Posted by brlukosk View Post
The problem I have run into is that the program does not know the difference from a valid and invalid puzzle.
That's obvious if you posted here. The problem is you aren't explaining what nor where the problem is. Is it ALL your tests? Just one? We didn't write it. We can't run it. You have to tell us what's wrong.

Originally Posted by brlukosk View Post
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.
Why not? What's going wrong? What area is it in? Did you output values at key places to figure out where the problem might be?

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: brlukosk is an unknown quantity at this point 
Solved Threads: 0
brlukosk brlukosk is offline Offline
Newbie Poster

Re: C++ function to check validity of sudoku

 
0
  #7
Nov 21st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ function to check validity of sudoku

 
0
  #8
Nov 21st, 2007
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.

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7. void readAPuzzle(int grid[] [9]);
  8. bool search(int grid[] [9]);
  9. void printGrid(int grid[] [9]);
  10. bool isValid(int grid[] [9]);
  11.  
  12. int main()
  13. {
  14. // Read a Sudoku puzzle
  15. int grid[9] [9];
  16.  
  17. readAPuzzle(grid);
  18.  
  19. printGrid(grid);
  20.  
  21. if (isValid(grid))
  22. cout << "Is a sudoku puzzle. " << endl;
  23.  
  24. else
  25. cout << "Is not a sudoku puzzle. " << endl;
  26.  
  27. return 0;
  28. }
  29.  
  30. //Reads Puzzle into 9x9 array ( modify to read from file )
  31. void readAPuzzle(int grid[] [9])
  32. {
  33. ifstream in_f;
  34.  
  35. in_f.open("ma1.dat");
  36.  
  37. for (int i = 0; i < 9; i++)
  38. for (int j = 0; j < 9; j++)
  39. in_f >> grid[i] [j];
  40. }
  41.  
  42. //Prints out the puzzle read from file (keep)
  43. void printGrid(int grid[] [9])
  44. {
  45. for (int i = 0; i < 9; i++)
  46. {
  47. for (int j = 0; j < 9; j++)
  48. cout << grid[i] [j] << " ";
  49. cout << endl;
  50. }
  51. }
  52. /** Check whether grid[i][j] is valid in the grid */
  53. bool isValid(int grid[] [9])
  54. {
  55. int i, j;
  56. bool status;
  57. status = true;
  58.  
  59. for (int column = 0; column < 9; column++)
  60. if (column != j && grid[i] [column] == grid[i] [j])
  61. status = false;
  62. cout << "1st test: " << status << endl;
  63.  
  64. for (int row = 0; row < 9; row++)
  65. if (row != i && grid[row] [j] == grid[i] [j])
  66. status = false;
  67. cout << "2nd test: " << status << endl;
  68.  
  69. for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
  70. for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
  71. if (row != i && col != j && grid[row] [col] == grid[i] [j])
  72. status = false;
  73. cout << "3rd test: " << status << endl;
  74.  
  75. for (int i = 0; i < 9; i++)
  76. for (int j = 0; j < 9; j++)
  77. if (grid[i][j] != 0)
  78. status = false;
  79. cout << "4th test: " << status << endl;
  80.  
  81. for (int i = 0; i < 9; i++)
  82. for (int j = 0; j < 9; j++)
  83. if ((grid[i][j] < 0) || (grid[i][j] > 9))
  84. status = false;
  85. cout << "5th test: " << status << endl;
  86.  
  87.  
  88. return status;
  89.  
  90. }
Look at the output and you should be able to tell which loop is working improperly.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: brlukosk is an unknown quantity at this point 
Solved Threads: 0
brlukosk brlukosk is offline Offline
Newbie Poster

Re: C++ function to check validity of sudoku

 
0
  #9
Nov 21st, 2007
It appears that the line:

  1. if (column != j && grid[i] [column] == grid[i] [j])

is incorrect.

How can I fix it?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4022 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC