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
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:
2 3 4 9 5 6 8 1 7 9 5 7 8 1 4 2 6 3 1 8 6 3 7 2 4 5 9 5 4 9 6 8 1 7 3 2 6 1 8 7 2 3 5 9 4 7 2 3 4 9 5 6 8 1 3 9 2 5 6 7 1 4 8 4 7 5 1 3 8 9 2 6 8 6 1 2 4 9 3 7 5
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:
2 3 4 9 5 6 8 1 7 9 5 7 8 1 4 2 6 3 1 8 6 3 7 2 4 5 9 5 4 9 6 8 1 7 3 2 6 1 8 7 2 3 5 9 4 7 2 3 4 9 5 6 8 1 3 9 2 5 6 7 1 4 8 4 7 5 1 3 8 9 2 6 8 6 1 2 4 9 3 7 5
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
#include
#include
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
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; }
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