need help.....
sudoku.h
class sudoku
{
public:
sudoku();
//default constructor
//Postcondition: grid is initialized to 0
sudoku(int g[][9]);
//constructor
//Postcondition: grid = g
void initializeSudokuGrid();
//Function to prompt the user to specify the numbers of the
//partially filled grid.
//Postcondition: grid is initialized to the numbers
// specified by the user.
void initializeSudokuGrid(int g[][9]);
//Function to initialize grid to g
//Postcondition: grid = g;
void printSudokuGrid();
//Function to print the sudoku grid.
bool solveSudoku();
//Funtion to solve the sudoku problem.
//Postcondition: If a solution exits, it returns true,
// otherwise it returns false.
bool findEmptyGridSlot(int &row, int &col);
//Function to determine if the grid slot specified by
//row and col is empty.
//Postcondition: Returns true if grid[row][col] = 0;
bool canPlaceNum(int row, int col, int num);
//Function to determine if num can be placed in
//grid[row][col]
//Postcondition: Returns true if num can be placed in
// grid[row][col], otherwise it returns false.
bool numAlreadyInRow(int row, int num);
//Function to determine if num is in grid[row][]
//Postcondition: Returns true if num is in grid[row][],
// otherwise it returns false.
bool numAlreadyInCol(int col, int num);
//Function to determine if num is in grid[row][]
//Postcondition: Returns true if num is in grid[row][],
// otherwise it returns false.
bool numAlreadyInBox(int smallGridRow, int smallGridCol,
int num);
//Function to determine if num is in the small grid
//Postcondition: Returns true if num is in small grid,
// otherwise it returns false.
private:
int grid[9][9];
};
//sudoku.cpp
#include <iostream>
#include "sudoku.h"
using namespace std;
sudoku::sudoku()
{
for (int row =0; row<9;row++)
for (int col=0; col<9; col++)
grid[row][col]=0;
}
sudoku::sudoku(int g[][9])
{
initializeSudokuGrid(g);
}
void sudoku::initializeSudokuGrid()
{
//Interactive
}
void sudoku::initializeSudokuGrid(int g[][9])
{
for (int row =0; row<9;row++)
for (int col=0; col<9; col++)
grid[row][col]=g[row][col];
}
void sudoku::printSudokuGrid()
{
for (int row =0; row<9;row++)
{
for (int col=0; col<9; col++)
cout<<grid[row][col]<<' ';
cout<<endl;
}
}
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; //backtrack
}
}
return false;
}
else
return true; //there are no empty slots
}
bool sudoku::findEmptyGridSlot(int &row, int &col)
{
for (row=0; row<9;++row)
for(col=0; col<9;++col)
if(grid[row][col]==0)
return true;
return false;
}
bool sudoku::canPlaceNum(int row, int col, int num)
{
//you can place number if number is not already in a row,
col or box.
}
bool sudoku::numAlreadyInRow(int row, int num)
{
for (int col=0;col<9;++col)
if(true)
return true;
return true;
}
bool sudoku::numAlreadyInCol(int col, int num)
{
for (int row=0;row<9;++row)
if(true)
return true;
return true;
}
bool sudoku::numAlreadyInBox(int smallGridRow, int
smallGridCol,
int num)
{
int startrow = smallGridRow - smallGridRow%3;
int endrow = startrow +2;
int startcol = smallGridCol - smallGridCol%3;
int endcol = startcol +2;
for (int row =startrow;row<=endrow;++row)
for(int col=startcol;col<=endcol;++col)
if (grid[row][col]==num)
return true;
return false;
}
//sudoku.cpp
#include <iostream>
#include <cmath>
#include "sudoku.h"
using namespace std;
int main()
{
sudoku s;
s.printSudokuGrid();
cout<<endl;
s.solveSudoku();
s.printSudokuGrid();
cout<<endl;
s.solveSudoku();
s.printSudokuGrid();
cout<<endl;
return 0;
}
sudoku::sudoku(int g[][9]) { for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ grid[i][j] = g[i][j]; } } } void sudoku::initializeSudokuGrid() { while(true){ int row,col,num; cout<<"Enter in the format row col num, enter a row/col number greater than 8 to exit"; cin>>row>>col>>num; if(row>8) break; if(col>8) break; grid[row][col] = num; } } void sudoku::initializeSudokuGrid(int g[][9]) { for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ grid[i][j] = g[i][j]; } } } void sudoku::printSudokuGrid() { /// Iterate over the elements and print! for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ cout<<grid[i][j]<<" "; } cout<<endl; } } 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; //backtrack } } return false; } else return true; //there are no empty slots } bool sudoku::findEmptyGridSlot(int &row, int &col) { return grid[row][col] == 0; } bool sudoku::canPlaceNum(int row, int col, int num) { int smallgridRow = (row/3)*3; int smallgridCol = (col/3)*3; return !numAlreadyInRow(row,num) && !numAlreadyInCol(col,num) && !numAlreadyInBox(smallgridRow,smallgridCol,num); } bool sudoku::numAlreadyInRow(int row, int num) { for(int i=0;i<9;i++){ if(grid[row][i] == num) return true; } return false; } bool sudoku::numAlreadyInCol(int col, int num) { for(int i=0;i<9;i++){ if(grid[i][col] == 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[smallGridRow+i][smallGridCol+j] == num) return true; } } return false; }
need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0...
I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...
Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...
I'm making a sudoku solver to check if the sudoku grid created
is legal. There are supposed to be no repeated numbers in each row,
column, and block. I got the code for the repeated numbers for row
and column but confused on how to write the code for the blocks.
Here is my code:
134 135 136 / COMPLETE THIS 137 // Returns true if this grid is legal. A grid is legal if no row, column, or //...
Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...
The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public: ...
how to randomly generate moves until the whole board is filled?. If the board size is c*r, you can only call the random function rand() c*r times (excluding the random function rand() that have been used in the provided code). Below is the code I need to convert. It is manual right now (user inputs numbers until whole board filled). I also have a piece of algorithmn below..but am unsure of how I can use it in the code: //Algorithm...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...
Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...
Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...
LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...