Question

I need help with the following and written in c++ thank you!: 1) replace the 2D...

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 : 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();
   Sudoku(int g[][9]);
   void initializeSudokuGrid();
   void initializeSudokuGrid(int g[][9]);
   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 "sudoku.h"
using namespace std;

Sudoku::Sudoku(void)
{  
   initializeSudokuGrid();
}
Sudoku::Sudoku(int g[][9])
{
   initializeSudokuGrid(g);
}
void Sudoku::initializeSudokuGrid()
{
   for(int row = 0; row < 9; row++)
       for(int col = 0; col < 9; col++)
           grid[row][col] = 0;
}
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 = 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 "Sudoku.h"
using namespace std;

int main()
{
   int g[][9] =
   {
       {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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) To replace the 2D array with vectors just add  vector<vector <int> > grid in place of 2D array declaration.

2) Here is the comstrcutor that reads from the file

Sudoku::Sudoku(void)
{   
int value;
ifstream infile;
infile.open("File Name");
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();  
   }
}

3) Here is the function the prints

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] << " ";
  
}
}
}

For any further doubts or clarification, comment down !

Add a comment
Know the answer?
Add Answer to:
I need help with the following and written in c++ thank you!: 1) replace the 2D...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0...

    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....

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    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...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    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:    ...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    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...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • I'm making a sudoku solver to check if the sudoku grid created is legal. There are...

    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 //...

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT