sudoku solving in c++ using class [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This sudoku solving program is getting compiled but getting error "segmentation fault " please help me in solving this problem and clarify why i am getting segmentation error as i have written every thing properlu .thanks in advance
#include<iostream>
#include<math.h>
class sudoku
{
public:
sudoku();
void initializeSudokuGrid();
void printSudokuGrid();
bool solveSudoku();
bool findEmptyGridSlot(int &row, int &col);
bool canPlaceNum(int row, int col, int num);
bool numAlreadyInRow(int row, int num);
bool numAlreadyInCol(int col, int num);
bool numAlreadyInBox(int smallGridRow, int smallGridCol, int num);
int grid[9][9];
};
sudoku::sudoku()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
grid[i][j] = 0;
}
}
std::cout << "\n all the grid locations are initialise to zero";
}
void sudoku::initializeSudokuGrid()
{
char x = 'y';
while (x == 'y')
{
int row, col, var;
std::cout
<< "\n enter the row,column and integer in the box(that is 1-9 numbers \n";
std::cin >> row;
std::cin >> col;
std::cin >> var;
grid[row][col] = var;
std::cout
<< "\n are there any slots that u want to enter the numbers into the boxs enter y else enter n \n";
std::cin >> x;
}
}
void sudoku::printSudokuGrid()
{
std::cout << "\n";
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
std::cout << grid[i][j] << " ";
}
std::cout << "\n";
}
}
bool sudoku::solveSudoku()
{
int row, col;
if (findEmptyGridSlot(row, col))
{
for (int num = 1; num <= 9; num++)
{
if (canPlaceNum(row, col, num))
{
grid[row][col] = num;
if (solveSudoku()) //recursive call
return true;
grid[row][col] = 0;
}
}
return false; //backtrack
}
else
return true; //there are no empty slots
}
bool sudoku::numAlreadyInRow(int row, int num)
{
for (int i = 0; i < 9; i++)
{
if (num != 0 && grid[row][i] == num)
return true;
}
return false;
}
bool sudoku::numAlreadyInCol(int col, int num)
{
for (int i = 0; i < 9; i++)
{
if (num != 0 && grid[i][col] == num)
return true;
}
return false;
}
bool sudoku::canPlaceNum(int row, int col, int num)
{
if (!numAlreadyInRow(row, num))
{
if (!numAlreadyInCol(col, num))
{
int smallGridRow = row - row % 3;
int smallGridCol = col - col % 3;
if (!numAlreadyInBox(smallGridRow, smallGridCol, num))
{
return true;
}
}
}
return false;
}
bool sudoku::numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (grid[i + smallGridRow][j + smallGridCol] == num)
return true;
}
}
return false;
}
bool sudoku::findEmptyGridSlot(int &row, int &col)
{
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
if (grid[row][col] == 0)
return true;
}
}
return false;
}
int main()
{
sudoku s;
s.printSudokuGrid();
s.initializeSudokuGrid();
s.printSudokuGrid();
std::cout << "\n after solving the problem \n";
if (s.solveSudoku())
s.printSudokuGrid();
else
std::cout << "\n solution doesnt exist for this type of solution \n";
return 0;
}

There's a mistake in your findEmptyGridSlot function
bool sudoku::findEmptyGridSlot(int &row, int &col)
{
for(int row=0;row<9;row++)
{
for(int col=0;col<9;col++)
{
...
You're declaring new int variables row and col in the loops, but you probably meant to use the ones passed as parameters.
Remove the int keyword to use the parameters instead of declaring new variables:
for(row=0;row<9;row++)
{
for(col=0;col<9;col++)
{

Related

5 queens on 8x8 board using backtracking

#include <iostream>
#include <fstream>
using namespace std;
void PrintSolution(int board[8][8], ofstream& out, int& count)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++) {
out << board[i][j] << " ";
}
out << endl;
}
out << endl;
count++;
}
int IsSafely(int board[][8], int row, int col)
{
for (int i = 0; i < row; i++)
{
if (board[i][col] == 1)
{
return 0;
}
}
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
{
if (board[i][j] == 1)
{
return 0;
}
}
for (int i = row, j = col; i >= 0 && j < 8; i--, j++)
{
if (board[i][j] == 1)
{
return 0;
}
}
return 1;
}
void FindSolution(int board[][8], int row, ofstream& out, int& count)
{
if (row == 8)
{
PrintSolution(board, out, count);
return;
}
for (int i = 0; i < 8; i++)
{
if (IsSafely(board, row, i))
{
board[row][i] = 1;
FindSolution(board, row + 1, out, count);
board[row][i] = 0;
}
}
}
void FillTheBoard(int board[][8])
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
board[i][j] = 0;
}
}
}
int main()
{
int board[8][8];
int count = 0;
ofstream output;
output.open("out.txt");
FillTheBoard(board);
FindSolution(board, 0, output, count);
output << count << endl;
output.close();
}
Hello everyone, I have a program that counts the number of arrangements of 8 queens on an 8x8 chessboard. I need to make a program to count the number of arrangements for 5 queens on an 8x8 board. Could you help me? What can I change in my program? For 8 queens, the program works correctly, it gives 92 solutions. For 5 queens, there should be 728 solutions on the 8x8 board.

Magic Number output

Alright so I have created this code. However, when i run it, it stops when it displays 104 for the counter??? I am so frustrated because I don't know how this could happen. The purpose of the code is to do the typical magic number output where the rows all add up to the same thing, the columns all add up to the same thing, and the diaganols all add up to the same thing. I believe the functions to do these calculations are correct, but the counter keeps stopping short of the 10000 attempts I am trying to do.
#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
void getrandom();
void insertnumber(int n);
bool magic();
void create();
const int rows = 3;
const int cols = 3;
int arr[rows][cols] = { {0,0,0}, {0,0,0} , {0,0,0} };
int main() {
int counter = 0;
do
{
counter++;
cout << counter << endl;
getrandom();
if (counter == 100000)
break;
} while (!magic());
create();
cout << "It took " << counter << " tries." << endl;
return 0;
}
void getrandom() {
int n = 0;
const int size = 9;
int oldnum[size];
for (int i = 0; i < rows * cols; i++) {
oldnum[i] = 0;
}
srand(time(NULL)); // had to import the new libraries to use this
bool used = true;
for (int i = 0; i < size; i++) {
do
{
used = true;
n = rand() % 9 + 1;
if (oldnum[n - 1] == 0)
{
oldnum[n - 1] = n;
used = false;
}
} while (used);
insertnumber(n);
}
}
void insertnumber(int n) {
for (int i = 0; i < rows; i++) {
for (int j = 0; i < cols; j++) {
if (arr[i][j] == 0) {
arr[i][j] = n;
return;
}
}
}
}
bool magic() {
int rowsum = arr[0][0] + arr[0][1] + arr[0][2];
for (int i = 1; i < cols; i++)
{
if (arr[i][0] + arr[i][1] + arr[i][2] != rowsum)
return false;
}
for (int j = 0; j < rows; j++)
{
if (arr[0][j] + arr[1][j] + arr[2][j] != rowsum)
return false;
}
if (arr[0][0] + arr[1][1] + arr[2][2] != rowsum)
return false;
if (arr[0][2] + arr[1][1] + arr[2][0] != rowsum)
return false;
return true;
}
void create() {
{
for (int i = 0; i < rows; i++) {
for (int j = 0; i < cols; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
}
You can try using a debugger for such problems.
I think you code crashes because of this:
for (int i = 0; i < rows; i++) {
for (int j = 0; i < cols; j++) {
It looks like you mean j < cols here :)
Check line 76. When I compile and run the code, line 76 is where the exception is thrown.
This line specifically
arr[i][j] = n;
It seems your insertnumber() function is the culprit.

Not receiving input from user for 3x3 sliding tile slide

I'm trying to create a 3x3 sliding tile puzzle game and I made the puzzle in a 2d array and now im trying to get the puzzle to slide a tile.
I'm new to debugging and cannot find out why the program isn't receiving the users input for the slide.
#define SLIDE_UP 1
#define SLIDE_DOWN 2
#define SLIDE_LEFT 3
#define SLIDE_RIGHT 4
void InitializeBoard(int[NUM_ROWS][NUM_COLS]);
void PrintBoard(int[NUM_ROWS][NUM_COLS]);
bool slideTile(int[NUM_ROWS][NUM_COLS], int);
void scrambleBoard(int[NUM_ROWS][NUM_COLS]);
bool isBoardSolved(int[NUM_ROWS][NUM_COLS]);
void DeallocateMemory(int[NUM_ROWS][NUM_COLS]);
int** ppRootPointer = NULL;
int main() {
int slidingBoard[NUM_ROWS][NUM_COLS];
char keyStroke = ' ';
int directionCode = UNSET;
int slideDirection = 0;
InitializeBoard(slidingBoard);
PrintBoard(slidingBoard);
slideTile(slidingBoard,slideDirection);
PrintBoard(slidingBoard);
_getch();
DeallocateMemory(slidingBoard);
return 0;
}
void InitializeBoard(int theBoard[NUM_ROWS][NUM_COLS]) {
ppRootPointer = new(int*[NUM_COLS]);
for (int i = 0; i < NUM_COLS; i++) {
ppRootPointer[i] = new(int[NUM_ROWS]);
}
int counter = 1;
int i = 0, j = 0;
for (i = 0; i < NUM_COLS; i++) {
for (j = 0; j < NUM_ROWS; j++) {
ppRootPointer[i][j] = counter++;
}
}
ppRootPointer[i-1][j-1] = PIVOT;
}
void PrintBoard(int theBoard[NUM_ROWS][NUM_COLS]) {
cout << left;
for (int i = 0; i < NUM_COLS; i++) {
for (int j = 0; j < NUM_ROWS; j++) {
if (ppRootPointer[i][j] != PIVOT) {
cout << setw(3) << ppRootPointer[i][j];
}
else {
cout << setw(3) << (char)PIVOT;
}
}
cout << endl;
}
cout << endl << endl;
}
bool slideTile(int theBoard[NUM_ROWS][NUM_COLS], int slideDirection) {
int pivotRow=0;
int pivotCol=0;
bool slidebool;
//once I declare i and j before the loop they go up to 3 instead of 2
//which causes the loop to not work
int i = 0;
int j = 0;
for (i = 0; i < NUM_COLS; i++) {
for (j = 0; j < NUM_ROWS; j++) {
if (theBoard[i][j] == (char)PIVOT) {
pivotCol = i;
pivotRow = j;
break;
}
}
}
//These are to assign pivotRow and Col because they dont get assigned
//inside of the loop
pivotCol = i;
pivotRow = j;
cout << "please enter a number to slide the tile (1 = up, 2 = down, 3 =
left, 4 = right" << endl;
cin >> slideDirection;
if (slideDirection == SLIDE_UP) {
if (pivotRow + 1 > NUM_ROWS) {
slidebool = false;
}
else {
ppRootPointer[pivotRow + 1][pivotCol] = PIVOT;
ppRootPointer[pivotRow][pivotCol] = ppRootPointer[pivotRow + 1]
[pivotCol];
slidebool = true;
}
}
.I'm hoping for the user to be able to enter an integer 1-4 representing the slide for the tile. This would allow me to build the scramble board function and get closer to finishing the code. Thanks to anyone that reads this and tries to help!!

c++ sudoku checker row, column, sub square

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
const int MAX = 9;
typedef char BOARD[MAX][MAX];
void dis_board(BOARD);
void new_board(BOARD, ifstream& in);
void add();
void remove(BOARD);
bool chk_row(BOARD arr, int row, int col);
bool chk_col(BOARD arr, int row, int col);
bool chk_sqr(BOARD arr, int row, int col);
int main()
{
BOARD board;
int row=0, col=0, num_ele = 0, num;
char ch1;
ifstream infile;
infile.open("mp6in.txt");
infile.get(ch1);
while (infile)
{
if (ch1 == 'N')
new_board(board, infile);
if (chk_row == false)
cout << "There is an error" << endl;
if (chk_col == false)
cout << "There is an error" << endl;
chk_sqr(board, 0, 0);
if (chk_sqr == false)
cout << "There is an error" << endl;
if (ch1 == 'D')
dis_board(board);
infile.get(ch1);
}
}
void dis_board(BOARD arr)
{
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
cout << setw(3) << arr[i][j];
cout << endl;
}
cout << endl;
}
void new_board(BOARD arr, ifstream& in)
{
in.ignore(100, '\n');
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
in.get(arr[i][j]);
in.ignore(100, '\n');
}
}
bool chk_row(BOARD arr, int row, int col)
{
for (int i = 0; i < MAX; i++)
{
if (i != col)
{
if (arr[row][i] == arr[row][col])
{
return false;
}
}
}
return true;
}
bool chk_col(BOARD arr, int row, int col)
{
for (int i = 0; i < MAX; i++)
{
if (i != row)
{
if (arr[i][col] == arr[row][col])
{
return false;
}
}
}
return true;
}
bool chk_sqr(BOARD arr, int row, int col)
{
int asquare = row / 3;
int bsquare = col / 3;
for (int i = asquare * 3; i < (asquare * 3 + 3); i++)
{
for (int j = bsquare * 3; j < (bsquare * 3 + 3); j++)
{
if (!(i == row && j == col))
{
if (arr[row][col] == arr[i][j])
{
return false;
}
}
}
}
return true;
}
Just in case I'm talking to anyone besides myself. I tried implementing the following checking functions and of course they aren't working correctly. For instance I know my first board has a duplicate number in the lower right block but my function can't find it.
chk_row is a function - you need to call it. Currently you are just checking with:
if (chk_row == false)
This is just checking if the function pointer is false, which is never going to happen. You need to call the function like this:
if (chk_row(board, row, col) == false)
You will need to work out what row and col should be. Probably you need a loop to check all the rows and columns, but I figure this is homework, so you should try it yourself.

Work with arrays

I'm writing some game at c++. The problem is with arrays. It always shows the 'desk' with points (empty parts). Need comments are in the code:
void showDesk(int someArray[][3])
{
for(int i = 0; i < 3 ;i++)
{
for (int j = 0; j < 3; j++)
{
if(someArray [i][j] == 0) cout << ".";
else if (someArray[i][j] == 1) cout << "x";
else if (someArray[i][j] == 2) cout << "o";
}
cout<<"\n";
}
}
void newDesk (int someArray[][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
someArray [i][j] = 0;
}
bool empty(int someArray[][3], int x, int y)
{
if (someArray[x][y] == 0)
return true; // It returns true
else
return false;
}
void setMark(int someArray[][3],int x,int y,int mark)
{
if (empty(someArray,x,y))
someArray [x][y] == mark; // But this never calls!
}
int main(int argc, char** argv) {
int x,y;
int mark;
int someArray [3][3];
newDesk(someArray);
showDesk(someArray);
cout << "------------------\n";
while (true)
{
cout<< "put x,y\n";
cin >> x>>y;
cout << "put mark\n";
cin >> mark;
setMark(someArray,x,y,mark);
showDesk(someArray);
}
return 0;
}
== is comparison, = is assignment.
Your line:
someArray [x][y] == mark;
Does nothing. It should be:
someArray [x][y] = mark;
someArray [x][y] == mark tests the array to see if its equal to mark, but never actually sets it to anything...