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

I am stuck in this program. Can someone help me complete this? I have done some

ID: 3708289 • Letter: I

Question

I am stuck in this program. Can someone help me complete this? I have done some part of it and completely lost

The C++ program is below and I have posted my code as well.

Program

Write a class called Verifier that can be used to verify whether a Sudoku puzzle solution is correct (i.e., whether or not it meets the conditions outlined above).

As with Assignment 2, the class definition for the Verifier class should be placed in a header file (Verifier.h) while the method definitions should be placed in their own source code file (Verifier.cpp).

Data members

The Verifier class should have a private data member to represent a Sudoku grid as a two-dimensional array of 9 rows, each with 9 columns. The exact data type of the array elements is up to you; you might decide to store the digits of the Sudoku grid as integers, or you may choose to store them as characters.

You may create other private data members for the class if you want to.

Methods

The Verifier class should have the following public methods. You are welcome to create additional private methods for the class if you want to.

Verifier default constructor - This constructor has no parameters. It should set all of the elements of the grid array to 0.

readGrid() - This method takes one parameter: a pointer to a constant character (data type const char*), which will point to an array of characters that contains the name of a file to use as input. It returns nothing. The method should read the contents of the input file into the elements of the grid array.

An input file contains exactly 81 numbers, arranged in 9 rows of 9 columns each, separated by whitespace. For example:

printGrid() - This method takes no arguments and returns nothing. It should print the Sudoku grid array to the screen as 9 rows of 9 columns (the same way the grid appears in the input file). For example:

verifySolution() - This method takes no arguments. It should return a Boolean value - true if the Sudoku grid array contains a valid solution, false if not.

My code is:

//Verifier.h

#ifndef VERIFIER_H

#define VERIFIER_H

class Verifier

{

private:

int gridArray[9][9];

public:

Verifier();

readGrid(const char* fileName[9][9] );

printGrid();

bool VerifySolution();

};

#endif

//Verifier.cpp

#include "Verifier.h"

#include <iostream>

#include <cstring>

#include <iomanip>

using namespace std;

//default constructor

Verifier::Verifier()

{

gridArray[9][9] = {0};

}

Verifier::readGrid(const char* fileName[9][9]) {

ifstream inputFile;

inputFile.open(fileName[9][9]);//opens the file

if(!fileName){

cout << "cannot open the file" << endl;

}

cout << "Reading from the file ";

// loop through the 1st dimension of the array

for ( unsigned i = 0; i < 9; i++ )

{

for ( unsigned j = 0; j < 9; j++ ) {// loop through the 2nd dimension of the array

inputFile >> fileName[i][j]; // read to each element of the array  

}

cout << ' ';

}

inputFile.close();//closes the file

cout << fileName[9][9];

}

Verifier::printGrid(){

for ( unsigned i = 0; i < 9; i++ ){

for ( unsigned j = 0; j < 9; j++ ) {// loop through the 2nd dimension of the array

cout << gridArray[i][j] << " ";

}

cout << endl;

}

void Verifier::verifySolution(){

//Searches the grid to find an entry that is still unassigned

for (int row = 0; row < 9; row++){

for (int col = 0; col < 9; col++){

if (gridArray[row][col] == UNASSIGNED){

return true;

}

else

return false;

}

}

//Returns a boolean which indicates whether any assigned entryin the specified row matches the given number.

for (int col = 0; col < 9; col++)

if (gridArray[row][col] == num)

return true;

return false;

}

}

//mainprogram.cpp
#include <iostream>
#include <string>
#include "Verifier.h"

using std::cout;
using std::endl;
using std::string;

#define NUM_FILES 7

int main()
{
Verifier v;
string fileName;

cout << "Sudoku Verifier ";

for (int i = 1; i <= NUM_FILES; i++)
{
cout << endl;

// Construct file pathname
fileName = string("/home/turing/t90kjm1/CS241/Data/Spring2018/Assign3/solution")
+ (char)('0' + i) + ".txt";
  
// Read the solution file as input
v.readGrid(fileName.c_str());

// Print the Sudoku grid
v.printGrid();
  
// Verify whether or not the solution is correct
if (v.verifySolution())
cout << " This is a valid Sudoku solution ";
else
cout << " This is not a valid Sudoku solution ";
}

return 0;
}

Explanation / Answer


Given below is the completed code for the question. As the question mentions that additional helper private functions can be implemented, I have used 3 more helper functions so that the code is simple and easy to understand.
Please do rate the answer if it was helpful. Thank you


//Verifier.h

#ifndef VERIFIER_H
#define VERIFIER_H
class Verifier
{
private:

int grid[9][9];

//helper private functions
bool checkRow(int row);
bool checkColumn(int col);
bool checkSubGrid(int row, int col);

public:

Verifier();
void readGrid(const char fileName[]);
void printGrid();
bool verifySolution();

};

#endif


Verifier.cpp
===========
//Verifier.cpp

#include "Verifier.h"
#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;

//default constructor
Verifier::Verifier()
{

for(int row = 0; row < 9; row++)
for(int col = 0; col < 9; col++)
grid[row][col] = 0;

}

void Verifier::readGrid(const char fileName[]) {
ifstream inputFile;

inputFile.open(fileName);//opens the file

if(inputFile.fail()){
cout << "cannot open the file " << fileName << endl;
return;
}

cout << "Reading from the file " << fileName << endl;

// loop through the 1st dimension of the array
for ( unsigned i = 0; i < 9; i++ )
{
for ( unsigned j = 0; j < 9; j++ ) {// loop through the 2nd dimension of the array
inputFile >> grid[i][j]; // read to each element of the array
}
}

inputFile.close();//closes the file
}

void Verifier::printGrid(){

for ( unsigned i = 0; i < 9; i++ ){
for ( unsigned j = 0; j < 9; j++ ) {// loop through the 2nd dimension of the array
cout << grid[i][j] << " ";
}

cout << endl;
}

}


bool Verifier::verifySolution(){

//Searches the grid to make sure an entry is in range 1-9
for (int row = 0; row < 9; row++){
for (int col = 0; col < 9; col++){

if (grid[row][col] < 1 || grid[row][col] > 9){
return false;
}
}
}


//check rows
for(int row = 0; row < 9; row++)
{
if(!checkRow(row))
return false;
}

//check columns
for(int col = 0; col < 9; col++)
{
if(!checkColumn(col))
return false;
}


//check sub grids
for(int row = 0; row < 9; row += 3)
for(int col = 0; col < 9; col += 3)
{
if(!checkSubGrid(row, col))
return false;
}

//all checks passed
return true;


}

//helper private functions
bool Verifier::checkRow(int row)
{
bool found[9] = {false};

for(int col = 0; col < 9 ; col++)
{
int index = grid[row][col];
if(found[index] == true) //already the number was seen
return false;
else
found[index] = true;
}

return true;
}
bool Verifier::checkColumn(int col)
{
bool found[9] = {false};

for(int row = 0; row < 9 ; row++)
{
int index = grid[row][col];
if(found[index] == true) //already the number was seen
return false;
else
found[index] = true;
}

return true;


}
bool Verifier::checkSubGrid(int row, int col)
{
bool found[9] = {false};

for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
int index = grid[row+i][col+j];
if(found[index] == true) //already the number was seen
return false;
else
found[index] = true;
}

return true;
}

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