Skip to content

Commit 4f76e34

Browse files
committed
Remove Global Variables
Removed the rowIter, colIter global variables since it is considered good programming practise to avoid them as much as possible.
1 parent d5cbd3e commit 4f76e34

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Sudoku-Generator/sudoku-generator.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ using namespace std;
1919
class SudokuFrame{
2020

2121
int sudokuFrame[9][9]; //This pointer will hold all the values in the matrix.
22-
int rowIter, colIter; //iterator variables
23-
2422

2523
/**
2624
* @desc This constructor calls the menu() func to provide the menu. It also
@@ -60,6 +58,8 @@ class SudokuFrame{
6058
* @return none
6159
*/
6260
private:void setZeros(){
61+
int rowIter, colIter;
62+
6363
for(rowIter=0; rowIter<9; rowIter++){
6464
for(colIter=0; colIter<9; colIter++){
6565
sudokuFrame[rowIter][colIter]=0;
@@ -96,6 +96,7 @@ class SudokuFrame{
9696
*/
9797
public:void clearFrameFrom(int row, int col){
9898
int jcount=0;
99+
int rowIter, colIter;
99100

100101
for(rowIter=row; rowIter<9; rowIter++){
101102

@@ -265,7 +266,6 @@ class Possibilities{
265266
*/
266267
class SudokuSolver{
267268

268-
int rowIter, colIter; //Iter variables
269269
int recursiveCount; //Stats variable
270270
SudokuFrame frame; //The frame object
271271

@@ -297,6 +297,7 @@ class SudokuSolver{
297297
* @return (bool) whether the value is valid or not in the sudoku frame
298298
*/
299299
private:bool cellValueValid(int row, int col, int currentValue){
300+
int rowIter, colIter;
300301

301302
//Checking if value exists in same column
302303
for(rowIter=0; rowIter<9; rowIter++){
@@ -333,6 +334,8 @@ class SudokuSolver{
333334

334335
int colStart=(col/3)*3;
335336
int colEnd=(colStart+2);
337+
338+
int rowIter, colIter;
336339

337340
for(rowIter=rowStart; rowIter<=rowEnd; rowIter++){
338341
for(colIter=colStart; colIter<=colEnd; colIter++){

Sudoku-Solver/sudoku-solver.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class SudokuFrame{
2020

2121
int sudokuFrame[9][9]; //This pointer will hold all the values in the matrix.
2222
int editableFrame[9][9]; //This pointer will tell us all the values which are editable.
23-
int rowIter, colIter; //iterator variables
24-
2523

2624
/**
2725
* @desc This constructor calls the menu() func to provide the menu.
@@ -86,6 +84,8 @@ class SudokuFrame{
8684
private:void readFrameValues(){
8785
cout<<"\nEnter the specified value when prompted.\n";
8886
cout<<"Enter 0 if cell is empty.\n\n";
87+
88+
int rowIter, colIter;
8989

9090
for(rowIter=0; rowIter<9; rowIter++){ //Iterating over cells to read vals.
9191
for(colIter=0; colIter<9; colIter++){
@@ -128,6 +128,8 @@ class SudokuFrame{
128128

129129
ifstream sudokuFile; //Opening file for reading.
130130
sudokuFile.open(filename,ios::in);
131+
132+
int rowIter, colIter;
131133

132134
for(rowIter=0; rowIter<9; rowIter++){ //Iterating over file values.
133135
for(colIter=0; colIter<9; colIter++){
@@ -194,6 +196,7 @@ class SudokuFrame{
194196
*/
195197
public:void clearFrameFrom(int row, int col){
196198
int jcount=0;
199+
int rowIter, colIter;
197200

198201
for(rowIter=row; rowIter<9; rowIter++){
199202

@@ -217,6 +220,7 @@ class SudokuFrame{
217220
public:void displayFrame(){
218221

219222
cout<<"++=====================================++";
223+
int rowIter, colIter;
220224

221225
for(rowIter=0; rowIter<9; rowIter++){
222226
int cellIter=1;
@@ -395,7 +399,6 @@ class Possibilities{
395399
*/
396400
class SudokuSolver{
397401

398-
int rowIter, colIter; //Iter variables
399402
int recursiveCount; //Stats variable
400403
SudokuFrame frame; //The frame object
401404

@@ -427,6 +430,7 @@ class SudokuSolver{
427430
* @return (bool) whether the value is valid or not in the sudoku frame
428431
*/
429432
private:bool cellValueValid(int row, int col, int currentValue){
433+
int rowIter, colIter;
430434

431435
//Checking if value exists in same column
432436
for(rowIter=0; rowIter<9; rowIter++){
@@ -464,6 +468,8 @@ class SudokuSolver{
464468
int colStart=(col/3)*3;
465469
int colEnd=(colStart+2);
466470

471+
int rowIter, colIter;
472+
467473
for(rowIter=rowStart; rowIter<=rowEnd; rowIter++){
468474
for(colIter=colStart; colIter<=colEnd; colIter++){
469475
if(frame.getCellValue(rowIter,colIter)==currentValue) return false;

0 commit comments

Comments
 (0)