Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make modifications to the attached Sudoku solution that 1) replace the 2D arrays

ID: 3845012 • Letter: M

Question

Make modifications to the attached Sudoku solution that

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.

i have this so far but cant get it to work could really use some help.

Sudoku.h

#pragma once
#include <vector>
/* notes
sudoku()
default constructor
precondition : none
postcondition: grid is initialized to 0

sudoku(g[][9])
1-parameter constructor
precondition : g satisfies sudoku grid restrictions
postcondition: grid = g

void initialiizeSudokuGrid()
interactive function to prompt the user to specify the number of the partially filled grid
precondition : none
postcondition: grid is initialized to the number specified by the user

void initializeSudokuGrid(int g[][9])
function to initialize grid to g
precondition: g satisties sudoku grid restrictions
postcondition: grid = g

void printSudokuGrid()
function to print the sudoku

bool solveSudoku()
function to solve thesukoku problem
precondition : none
postcondition: if a solution exists, 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
precondition : row and col refer to a grid slot
postcondition: returns true if grid[row][col] = 0, otherwise it returns false

bool canPlaceNum(int row, int col, int num)
function to determine if num can be placed in grid[row][col]
precondition : row and col refer to a grid slot
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][]
precondition : row refers to a grid row and num is an integer inclusively between 1 and 9
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][]
precondition : col refers to a grid column and num is an integer inclusively between 1 and 9
postcondition: returns true if num is in grid[][col], otherwise it returns false

bool numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
function to determine if num is in the small grid that contains grid[smallGridRow][smallGridCol]
precondition : smallGridRow and smallGridCol refer to a grid slot, num is an integer inclusively between 1 and 9
postcondition: returns true if num is in small grid, otherwise it returns false
*/

class Sudoku
{
public:
    Sudoku();
    //void initializeSudokuGrid();
    //void initializeSudokuGrid(vector<vector<int>> g);
    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);
private:
    int grid[9][9];
};

Sudoku.cpp

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "sudoku.h"
using namespace std;

Sudoku::Sudoku(void)
{  
int value;
ifstream infile;
infile.open("Sudoku.txt");
vector <int> tempVec;
   for (int s=0; s<=8; s++){
for (int i=0; i<=8; i++){
infile >> value;
tempVec.push_back(value);
}
grid.push_back(tempVec);
       tempVec.clear();
   }
}
void Sudoku::initializeSudokuGrid()
{
    for(int row = 0; row < 9; row++)
        for(int col = 0; col < 9; col++)
            grid[row][col] = 0;
}
void Sudoku::initializeSudokuGrid(vector <vector<int>> g;)
{
    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 i=0;i<=8;i++)
{
for (int j=0;j<=8;j++)
{
if (grid[i][j]==0 || grid[i][j]=='0')
cout << " ";
else
cout << puzzle[i][j] << " ";

}
}
}
bool Sudoku::solveSudoku()
{
    int row = 0, col = 0;
    if(findEmptyGridSlot(row, col))
    {
        for(int num = 1; num <= 9; num++)
        {
            if(canPlaceNum(row, col, num))
            {
                grid[row][col] = num;
                if(solveSudoku())
                    return true;
                grid[row][col] = 0;
            }
        }
        return false;
    }
    else
        return true;
}
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;
    row = -1;
    col = -1;
    return false;
}
bool Sudoku::canPlaceNum(int row, int col, int num)
{
    return !numAlreadyInRow(row, num) &&
           !numAlreadyInCol(col, num) &&
           !numAlreadyInBox(row, col, num);
}
bool Sudoku::numAlreadyInRow(int row, int num)
{
    for(int col = 0; col < 9; col++)
        if(grid[row][col] == num) return true;
    return false;
}
bool Sudoku::numAlreadyInCol(int col, int num)
{
    for (int row = 0; row < 9; row++)
        if (grid[row][col] == num) return true;
    return false;
}
bool Sudoku::numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
{
    int beginSmallGridRow = smallGridRow - smallGridRow % 3;
    int endSmallGridRow = beginSmallGridRow + 3;
    int beginSmallGridCol = smallGridCol - smallGridCol % 3;
    int endSmallGridCol = beginSmallGridCol + 3;

    for (int row = beginSmallGridRow; row < endSmallGridRow; row++)
        for (int col = beginSmallGridCol; col < endSmallGridCol; col++)
            if (grid[row][col] == num)
                return true;
    return false;
}

Source.cpp

#include <iostream>
#include <string>
#include <vector>
#include "Sudoku.h"
using namespace std;

int main()
{
    vector <vector<int>> g
    {
        {6,0,3,0,2,0,0,9,0},
        {0,0,0,0,5,0,0,8,0},
        {0,2,0,4,0,7,0,0,1},
        {0,0,6,0,1,4,3,0,0},
        {0,0,0,0,8,0,0,5,6},
        {0,4,0,6,0,3,2,0,0},
        {8,0,0,2,0,0,0,0,7},
        {0,1,0,0,7,5,8,0,0},
        {0,3,0,0,0,6,1,0,5}
    };
    Sudoku sudoku1;
    sudoku1.printSudokuGrid();
    sudoku1.solveSudoku();
    cout << endl;
    sudoku1.printSudokuGrid();
    cout << endl;
    //Sudoku sudoku2(g);
    //sudoku2.solveSudoku();
    //sudoku2.printSudokuGrid();
    //cout << endl;
    //sudoku2.solveSudoku();
    //sudoku2.printSudokuGrid();
    //cout << endl;
    system("pause");
    return 0;
}

Explanation / Answer

Use "g++ -std = c++11 source.cpp" to compile.

Source.cpp:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Sudoku
{
public:
Sudoku();
//void initializeSudokuGrid();
void initializeSudokuGrid(vector< vector<int> > g);
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);
private:
vector< vector<int> > grid;
};


Sudoku::Sudoku(void)
{

}

void Sudoku::initializeSudokuGrid(vector <vector<int> > g)
{
this->grid=g;
}

void Sudoku::printSudokuGrid()
{
for (int i=0;i<=8;i++)
{
for (int j=0;j<=8;j++)
{
if (this->grid[i][j]==0 || this->grid[i][j]=='0')
cout << " ";
else
cout << this->grid[i][j] << " ";

}
cout<<endl;
}
}


bool Sudoku::solveSudoku()
{
int row = 0, col = 0;
if(findEmptyGridSlot(row, col))
{
for(int num = 1; num <= 9; num++)
{
if(canPlaceNum(row, col, num))
{
grid[row][col] = num;
if(solveSudoku())
return true;
grid[row][col] = 0;
}
}
return false;
}
else
return true;
}

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;
row = -1;
col = -1;
return false;
}

bool Sudoku::canPlaceNum(int row, int col, int num)
{
return !numAlreadyInRow(row, num) &&
!numAlreadyInCol(col, num) &&
!numAlreadyInBox(row, col, num);
}

bool Sudoku::numAlreadyInRow(int row, int num)
{
for(int col = 0; col < 9; col++)
if(grid[row][col] == num) return true;
return false;
}

bool Sudoku::numAlreadyInCol(int col, int num)
{
for (int row = 0; row < 9; row++)
if (grid[row][col] == num) return true;
return false;
}

bool Sudoku::numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
{
int beginSmallGridRow = smallGridRow - smallGridRow % 3;
int endSmallGridRow = beginSmallGridRow + 3;
int beginSmallGridCol = smallGridCol - smallGridCol % 3;
int endSmallGridCol = beginSmallGridCol + 3;

for (int row = beginSmallGridRow; row < endSmallGridRow; row++)
for (int col = beginSmallGridCol; col < endSmallGridCol; col++)
if (grid[row][col] == num)
return true;
return false;
}

int main()
{
vector< vector<int> > g
{
{6,0,3,0,2,0,0,9,0},
{0,0,0,0,5,0,0,8,0},
{0,2,0,4,0,7,0,0,1},
{0,0,6,0,1,4,3,0,0},
{0,0,0,0,8,0,0,5,6},
{0,4,0,6,0,3,2,0,0},
{8,0,0,2,0,0,0,0,7},
{0,1,0,0,7,5,8,0,0},
{0,3,0,0,0,6,1,0,5}
};
Sudoku sudoku2;
sudoku2.initializeSudokuGrid(g);
sudoku2.solveSudoku();
sudoku2.printSudokuGrid();
cout << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote