Question
So I'm wondering why my code is outputting a text file that looks like When it should be outputting that looks like Here is the
So I'm wondering why my code is outputting a text file that looks like
When it should be outputting that looks like
Here is the code included:
#include
class Suduko { public: bool FindUnassignedLocation(int grid[9][9], int &row, int &col); //bool isSafe(int grid[9][9], int row, int col, int num); void readFile(int grid[9][9]); void writeFile(int grid[9][9]); bool SolveSudoku(int grid[9][9]); bool UsedInRow(int grid[9][9], int row, int num); bool UsedInCol(int grid[9][9], int col, int num); bool UsedInBox (int grid[9][9], int boxStartRow, int boxStartCol, int num); bool isSafe( int grid[9][9], int row, int col, int num); void printGrid(int grid[9][9]); //void writeFile(int sudokuBoard[9][9]); }; /********************************************************/
void Suduko::readFile(int grid[9][9]) { //Declare filename char sourceFile[256];
//Declare file input ifstream fin;
//Get filename from user cout > sourceFile;
//Open file with error checking fin.open(sourceFile); if (fin.fail()) { cout
//Read file into array for (int row = 0; row > grid[row][col]; if (grid[row][col] == '0') { grid[row][col] = ' '; } } } //Close the file fin.close(); }
/********************************************************/
/* assign values to all unassigned locations for Sudoku solution */ bool Suduko::SolveSudoku(int grid[9][9]) { int row, col; if (!FindUnassignedLocation(grid, row, col)) return true; for (int num = 0; num
/* Searches the grid to find an entry that is still unassigned. */ bool Suduko::FindUnassignedLocation(int grid[9][9], int &row, int &col) { for (row = 0; row
/* Returns whether any assigned entry n the specified row matches the given number. */ bool Suduko::UsedInRow(int grid[9][9], int row, int num) { for (int col = 0; col
/* Returns whether any assigned entry in the specified column matches the given number. */ bool Suduko::UsedInCol(int grid[9][9], int col, int num) { for (int row = 0; row
/* Returns whether any assigned entry within the specified 3x3 box matches the given number. */ bool Suduko::UsedInBox(int grid[9][9], int boxStartRow, int boxStartCol, int num) { for (int row = 0; row
/* Returns whether it will be legal to assign num to the given row,col location. */ bool Suduko::isSafe(int grid[9][9], int row, int col, int num) { return !UsedInRow(grid, row, num) && !UsedInCol(grid, col, num) && !UsedInBox(grid, row - row % 3 , col - col % 3, num); }
void Suduko::printGrid(int grid[9][9]) { for (int row = 0; row
/*********************************************************************************************/ void Suduko::writeFile(int grid[9][9]) { //Declare file output ofstream fout; char destinationFile[256];
//Asking for user input cout > destinationFile;
//Open destination file & error checking fout.open(destinationFile); if (fout.fail()) { cout
for (int col = 0; col
fout
//Makes sure it's a 9x9 grid if (col % 9 == 0) { fout
//Close file fout.close(); }
unsigned solution_time= 0;
/*********************************************************************************************/ int main() {
Suduko gr;
int grid[9][9]; gr.readFile(grid); //gr.writeFile(grid); clock_t t0,t1; t0=clock(); if(gr.SolveSudoku(grid) == true) { gr.printGrid(grid); t1=clock(); std::cout 8 5,9 7 2 6 1 3 7 1 2 86 39 5 49 6 3 5 1 4 2 7 8 1 79 3 4 5 8 6 2 6 2 4 1 9 8 7 3 53 5 8 7 26 4 9 1 5 47 2 39 1 8 6 2 3 1 6 8 7 5 49 8 96 4 51 3 2 7
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started