Public Group

# Vectors!

This topic is 3835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've got something like 400 lines of code for my Sudoku program at this point, and I realize that not too many people will be inclined to look at it. Thankfully, the majority of it works swell, and I only need part of it examined. I realize that my use of vectors is not right, and I would love for someone to take a peek at my code to show me what I'm doing wrong. I can provide some guidance about which parts should be looked at, as the code is incomplete and really won't make sense yet to anyone but myself. I'll provide an attachment for anyone kind enough to look. My algorithm works in such a way that completed boards are generated that follow the rules of Sudoku. Then, one by one, values inside cells are removed if the puzzle can still be solved in there absence. Functions that need some help are detailed as follows: 1. The return of the RecordValues function , which is called by the FinishBoard function. Currently the function has no return. I realize that this substitute Help with Code Tags
      vector<int> sureValues = RecordValues(FinalBoard, i, theRandom)


is inadequate. 2. I am curious if the CheckCell function is called properly. Additionally, I think the prototypes that have vectors as parameters need to be looked at. 3. The compiler is telling me that the code Help with Code Tags
      if (CheckCell(sureValues, row, column) == false)


is wrong because "conversion from 'int' to non-scalar type 'std::vect<int, std::allocater<int>' requested." Thanks for the help The full code is posted below.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream> // Stream class to write files.
#include <ctype.h> // allows the use of toupper.
#include <vector>  // Used for the final Sudoku board with removed numbers.
using namespace std;

/////////////  Define Function Prototypes //////////////////
int CheckCell(vector<int>& sureValues, int row, int column);
int RecordValues(const int Board[9][9], int row, int column);
void CreateBoard(int Board[9][9]);
void FinishBoard(int Board[9][9], int difficulty);
int Difficulty();
void Debug(int Board[9][9], bool BoardStatus[9][9], int row, int column, int BoardTemp);
void WriteFile(int Board[9][9]);
void Display(int Board[9][9]);
void Reset(int Board[9][9], bool BoardStatus[9][9]);
int Random_Number(); //  Used to create random integers.  This will ensure that each Sudoku board is unique.
bool Verify(int Board[9][9], int row, int column); // Determines whether the values within a given cell are valid or not.

///////////// Main ////////////////
int main()
{
int Board[9][9];
CreateBoard(Board);
int difficulty = Difficulty();
WriteFile(Board);
Display(Board);
FinishBoard(Board, difficulty);
cin.ignore();
return 0;
}
void FinishBoard(int FinalBoard[9][9], int difficulty) // scans columns and rows to determine if values within a cell can both be determined by humans and have a single solution.
{
if (difficulty >= 0) // Easy
{
for (int i = 0; i <= 8; i ++) // makes sure that one cell will be blank in each row.
{
int theRandom = Random_Number();
FinalBoard [theRandom];  // The cell whose value will be tested to see it can be removed.
vector<int> sureValues = RecordValues(FinalBoard, i, theRandom)  // i functions as the row while theRandom functions as the column.
if (CheckCell(sureValues, row, column) == false)
{
FinalBoard [theRandom] = " ";
}
}
}
if (difficulty >= 1) // Intermediate
{
for (int i = 0; i <= 8; i ++) // makes sure that one cell will be blank in each row.
{
int theRandom = Random_Number();
FinalBoard [theRandom]; // The cell whose value will be tested to see it can be removed.
}
}
if (difficulty >= 2) //  Hard
{
for (int i = 0; i <= 8; i ++) // makes sure that one cell will be blank in each row.
{
int theRandom = Random_Number();
FinalBoard [theRandom]; // The cell whose value will be tested to see it can be removed.
}
}
}
int CheckCell(vector<int>& sureValues, int row, int column)
{
;
}
int RecordValues(const int Board[9][9], int row, int column);
{
vector<int> sureValues;
for (i = 0; i <= 8; i++) // record row values.
{
if ( i != column && Board[row] != " ")
{
sureValues.pushback(Board[row]);
}
}
for (i = 0; i <= 8; i++) // record column values.
{
if ( i != row && Board[column] != " ")
{
sureValues.pushback(Board[column]);
}
}
if (column%3 == 0 && row%3 == 0) // Record cell values.
{
if (Board [row+1][column+1]) != " ")
{ sureValues.pushback(Board [row+1][column+1]) }
if (Board [row+2][column+1] != " ")
{ sureValues.pushback (Board [row+2][column+1]) }
if (Board [row+1][column+2] != " ")
{ sureValues.pushback(Board [row+1][column+2]) }
if (Board [row+2][column+2] != " ")
{ sureValues.pushback(Board [row+2][column+2]) }
}
if (column%3 == 1 && row%3 == 0)
{
if (Board [row+1][column-1]) != " ")
{ sureValues.pushback(Board [row+1][column-1]]) }
if (Board[row+2][column-1]] != " ")
{ sureValues.pushback (Board[row+2][column-1]) }
if (Board [row+1][column+1] != " ")
{ sureValues.pushback(Board [row+1][column+1]) }
if (Board [row+2][column+1] != " ")
{ sureValues.pushback(Board [row+2][column+1]) }
}
if (column%3 == 2 && row%3 == 0)
{
if (Board [row+1][column-1]) != " ")
{ sureValues.pushback(Board [row+1][column-1]) }
if (Board [row+2][column-1] != " ")
{ sureValues.pushback (Board [row+2][column-1]) }
if (Board [row+1][column-2] != " ")
{ sureValues.pushback(Board [row+1][column-2]) }
if (Board [row+2][column-2] != " ")
{ sureValues.pushback(Board [row+2][column-2]) }
}
if (column%3 == 0 && row%3 == 1)
{
if (Board [row-1][column+1]) != " ")
{ sureValues.pushback(Board [row-1][column+1]) }
if (Board [row-1][column+2] != " ")
{ sureValues.pushback (Board [row-1][column+2]) }
if (Board [row+1][column+1] != " ")
{ sureValues.pushback(Board [row+1][column+1]) }
if (Board [row+1][column+2]) != " ")
{ sureValues.pushback(Board [row+1][column+2]) }
}
if (column%3 == 1 && row%3 == 1)
{
if (Board [row-1][column-1]) != " ")
{ sureValues.pushback(Board [row-1][column-1]) }
if (Board[row+1][column-1] != " ")
{ sureValues.pushback (Board[row+1][column-1]) }
if (Board [row-1][column+1] != " ")
{ sureValues.pushback(Board [row-1][column+1]) }
if (Board [row+1][column+1] != " ")
{ sureValues.pushback(Board [row+1][column+1]) }
}
if (column%3 == 2 && row%3 == 1)
{
if (Board [row-1][column-2]) != " ")
{ sureValues.pushback(Board [row-1][column-2]) }
if (Board [row-1][column-1] != " ")
{ sureValues.pushback (Board [row-1][column-1]) }
if (Board [row+1][column-2] != " ")
{ sureValues.pushback(Board [row+1][column-2]) }
if (Board [row+1][column-1] != " ")
{ sureValues.pushback(Board [row+1][column-1]) }
}
if (column%3 == 0 && row%3 == 2)
{
if (Board [row-1][column+1]) != " ")
{ sureValues.pushback(Board [row-1][column+1]) }
if (Board [row-2][column+1] != " ")
{ sureValues.pushback (Board [row-2][column+1]) }
if (Board [row-1][column+2] != " ")
{ sureValues.pushback(Board [row-1][column+2]) }
if (Board [row-2][column+2] != " ")
{ sureValues.pushback(Board [row-2][column+2]) }
}
if (column%3 == 1 && row%3 == 2)
{
if (Board [row-1][column-1]) != " ")
{ sureValues.pushback(Board [row-1][column-1]) }
if (Board [row-2][column-1] != " ")
{ sureValues.pushback (Board [row-2][column-1]) }
if (Board [row-1][column+1] != " ")
{ sureValues.pushback(Board [row-1][column+1]) }
if (Board [row-2][column+1] != " ")
{ sureValues.pushback(Board [row-2][column+1]) }
}
if (column%3 == 2 && row%3 == 2)
{
if (Board [row-2][column-2]) != " ")
{ sureValues.pushback(Board [row-2][column-2]) }
if (Board [row-1][column-2] != " ")
{ sureValues.pushback (Board [row-1][column-2]) }
if (Board [row-2][column-1] != " ")
{ sureValues.pushback(Board [row-2][column-1]) }
if (Board [row-1][column-1] != " ")
{ sureValues.pushback(Board [row-1][column-1]) }
}
}
void CreateBoard(int Board[9][9])
{
int row = 0;
int column = 0;
bool BoardStatus [9][9];
int BoardTemp;
Reset(Board, BoardStatus);
for (row = 0; row <= 8; row++)
{
for (column = 0; column <= 8; column++)
{
while (BoardStatus [row][column] == false)
{
if (Board [row][column] == -1)
{
Board [row][column] = Random_Number();  // a random number is placed inside every cell before the validation process begins in order to randomize each puzzle.
BoardTemp = Board [row][column];
}
BoardStatus [row][column] = Verify(Board, row, column);
if (BoardStatus [row][column] == false)
{
Board [row][column] = Board[row][column]+ 1;
if (Board [row][column] == 10) // A value inside a cell cannot be greater than 9.
{
Board[row][column] = 1;
}
if (BoardTemp == Board[row][column] )
{
Reset(Board, BoardStatus);
row = 0;
column = 0;
}
}
}
}
}
}
int Difficulty()
{
cout << "Choose a difficulty among Easy, Intermediate, and Hard.";
string x;
int difficulty;
cin >> x;
for (int i =0; i <= x.size(); i++)
{ x = toupper(x); }
if (x == "EASY" )
{ difficulty = 0; }
if (x == "INTERMEDIATE" )
{ difficulty = 1; }
if (x == "HARD" )
{ difficulty = 2; }
return difficulty;
}

void WriteFile(int Board[9][9])
{
ofstream SudokuBoard;
SudokuBoard.open ("1.txt", fstream::app);
for (int row = 0; row <= 8; row++)
{
SudokuBoard << endl << "----------------------" << endl;
if ( row == 3 || row == 6)
{
SudokuBoard << "----------------------" << endl;
}
for (int column = 0; column <= 8; column++)
{
SudokuBoard << Board[row][column] << "|";  // prints horizontally.
if (column == 2 || column == 5)
{
SudokuBoard << "| ";
}
}
}
SudokuBoard << endl  << endl;
SudokuBoard.close();
}
void Debug (int Board[9][9], bool BoardStatus[9][9], int row, int column, int BoardTemp)
{
Display(Board);
cout << endl;
cout << "column" <<  column << endl;
cout << "row" << row << endl;
cout << "BoardTemp" << BoardTemp << endl;
cout << BoardStatus[row][column] << endl;
system("pause");
}
void Display(int Board[9][9])
{
for (int row = 0; row <= 8; row++)
{
cout << endl << "-----------------------------" << endl;
if ( row == 3 || row == 6)
{
cout << "-----------------------------" << endl;
}
for (int column = 0; column <= 8; column++)
{
cout << Board[row][column] << " |" ;  // prints horizontally.
if (column == 2 || column == 5)
{
cout << "| ";
}
}
}
}
void Reset(int Board[9][9], bool BoardStatus[9][9])
{
srand((unsigned)time(0));
for (int row = 0; row <= 8; row++)
{
for (int column = 0; column <= 8; column++)
{
Board [row][column] = -1;    //  Sets Board values to an impossible value to ensure that false positive comparisons are not made between cells.
BoardStatus [row][column] = false;
}
}
}
int Random_Number()
{
int random_integer = (rand()%9)+1;
return random_integer;
}

bool Verify(int Board[9][9], int row, int column)
{
for (int n=0; n <= 8; n++)// If the given cell equals a cell in its horizontal row or vertical column, the cell's status is set to false.
{
if  (n != row && Board [row][column] == Board [n][column])
{
return false;
}
if (n != column && Board [row][column] == Board [row][n])
{
return false;
}
}
if (column%3 == 0 && row%3 == 0) // the following lines test whether or not the value in the cell equals a value within that cell's respective 3column3 square.
{
if (Board [row][column] == Board [row+1][column+1] || Board [row][column] == Board [row+2][column+1] || Board [row][column] == Board [row+1][column+2] || Board [row][column] == Board [row+2][column+2])
{
return false;
}
}
if (column%3 == 1 && row%3 == 0)
{
if (Board [row][column] == Board [row+1][column-1] || Board [row][column] == Board[row+2][column-1] || Board [row][column] == Board [row+1][column+1] || Board [row][column] == Board [row+2][column+1])
{
return false;
}
}
if (column%3 == 2 && row%3 == 0)
{
if (Board [row][column] == Board [row+1][column-1] || Board [row][column] == Board [row+2][column-1] || Board [row][column] == Board [row+1][column-2] || Board [row][column] == Board [row+2][column-2])
{
return false;
}
}
if (column%3 == 0 && row%3 == 1)
{
if (Board [row][column] == Board [row-1][column+1] || Board [row][column] == Board [row-1][column+2] || Board [row][column] == Board [row+1][column+1] || Board [row][column] == Board [row+1][column+2])
{
return false;
}
}
if (column%3 == 1 && row%3 == 1)
{
if (Board [row][column] == Board [row-1][column-1] || Board [row][column] == Board[row+1][column-1] || Board [row][column] == Board [row-1][column+1] || Board [row][column] == Board [row+1][column+1])
{
return false;
}
}
if (column%3 == 2 && row%3 == 1)
{
if (Board [row][column] == Board [row-1][column-2] || Board [row][column] == Board [row-1][column-1] || Board [row][column] == Board [row+1][column-2] || Board [row][column] == Board [row+1][column-1])
{
return false;
}
}
if (column%3 == 0 && row%3 == 2)
{
if (Board [row][column] == Board [row-1][column+1] || Board [row][column] == Board [row-2][column+1] || Board [row][column] == Board [row-1][column+2] || Board [row][column] == Board [row-2][column+2])
{
return false;
}
}
if (column%3 == 1 && row%3 == 2)
{
if (Board [row][column] == Board [row-1][column-1] || Board [row][column] == Board[row-2][column-1] || Board [row][column] == Board [row-1][column+1] || Board [row][column] == Board [row-2][column+1])
{
return false;
}
}
if (column%3 == 2 && row%3 == 2)
{
if (Board [row][column] == Board [row-2][column-2] || Board [row][column] == Board [row-1][column-2] || Board [row][column] == Board [row-2][column-1] || Board [row][column] == Board [row-1][column-1])
{
return false;
}
}
return true;
}



##### Share on other sites
hi,

The sureValues is an empty vector that cannot hold anything. You need to either declare it to a certain size or use a method such as:

sureValues.pushBack(RecordValues(FinalBoard, i, theRandom))
vector<int> sureValues = RecordValues(FinalBoard, i, theRandom)

Also, you are not returning anything from the RecordValues() function.

Good luck.

##### Share on other sites
The RecordValues has an int return value instead of vector<int>.
Change this and return the vector from the function.

Also in your FinishBoard function, what do you do with that line:
FinalBoard [theRandom]; ? There's no assignment so unless you overloaded the [] operator nothing will happen [smile].

##### Share on other sites
alright. fixed a crapload of errors. At least 100 of them. Thank goodness for Microsoft word's replace feature.

So I've solved all of my issues minus 1. On line 187,
      return sureValues;

causes the error cannot convert 'std::vector<int, std::allocator<int> >' to 'int' in return. I've got a feeling that this error has to do something with my declaration of the function, which looks like this

      int RecordValues(const int Board[9][9], int row, int column)

Other than this one, I'm good to go.

After this i hope to implement some sort of backtracking algorithm to dramatically decrease the Sudoku board creation time.

Thanks for the help. By the way, should I be returning this vector as a reference maybe? Or is that not possible if the vector is created in a function, because the variable would become out of scope?

All of the redone code is attached below.

      vector<int> sureValues;      sureValues.push_back( RecordValues(FinalBoard, i, theRandom));

So, I'm guessing that since the return of RecordValues is a vector that I am able to push_back the contents of an entire vector into a new vector.

#include <iostream>#include <ctime>#include <cstdlib>#include <fstream> // Stream class to write files.#include <ctype.h> // allows the use of toupper.#include <vector>  // Used for the final Sudoku board with removed numbers.using namespace std;/////////////  Define Function Prototypes //////////////////int CheckCell(vector<int> &sureValues, int row, int column);int RecordValues(const int Board[9][9], int row, int column);void CreateBoard(int Board[9][9]);void FinishBoard(int Board[9][9], int difficulty);int Difficulty();void Debug(int Board[9][9], bool BoardStatus[9][9], int row, int column, int BoardTemp);void WriteFile(int Board[9][9]);void Display(int Board[9][9]);void Reset(int Board[9][9], bool BoardStatus[9][9]);int Random_Number(); //  Used to create random integers.  This will ensure that each Sudoku board is unique.bool Verify(int Board[9][9], int row, int column); // Determines whether the values within a given cell are valid or not.///////////// Main ////////////////int main(){    int Board[9][9];    CreateBoard(Board);     int difficulty = Difficulty();    WriteFile(Board);    Display(Board);    FinishBoard(Board, difficulty);      cin.ignore();    return 0;}void FinishBoard(int FinalBoard[9][9], int difficulty) // scans columns and rows to determine if values within a cell can both be determined by humans and have a single solution.{     if (difficulty >= 0) // Easy     {          for (int i = 0; i <= 8; i ++) // makes sure that one cell will be blank in each row.          {               int theRandom = Random_Number();               FinalBoard [theRandom];  // The cell whose value will be tested to see it can be removed.               vector<int> sureValues;                sureValues.push_back( RecordValues (FinalBoard, i, theRandom) );  // i functions as the row while theRandom functions as the column.               if (CheckCell(sureValues, i, theRandom) == false) // again, i functions as the row and theRandom as the column.               {                    FinalBoard [theRandom] = ' ';               }          }     }                    if (difficulty >= 1) // Intermediate     {          for (int i = 0; i <= 8; i ++) // makes sure that one cell will be blank in each row.          {               int theRandom = Random_Number();               FinalBoard [theRandom]; // The cell whose value will be tested to see it can be removed.          }     }     if (difficulty >= 2) //  Hard     {          for (int i = 0; i <= 8; i ++) // makes sure that one cell will be blank in each row.          {               int theRandom = Random_Number();                FinalBoard [theRandom]; // The cell whose value will be tested to see it can be removed.          }     }}int CheckCell(vector<int>& sureValues, int row, int column){return 0;   ///////////// Work in progress}int RecordValues(const int Board[9][9], int row, int column){     vector<int> sureValues;     for (int i = 0; i <= 8; i++) // record row values.     {          if ( i != column && Board[row] != ' ')          {               sureValues.push_back(Board[row]);          }     }     for (int i = 0; i <= 8; i++) // record column values.     {          if ( i != row && Board[column] != ' ')          {               sureValues.push_back(Board[column]);          }     }     if (column%3 == 0 && row%3 == 0) // Record cell values.     {        if (Board [row+1][column+1] != ' ')        { sureValues.push_back(Board [row+1][column+1]); }        if (Board [row+2][column+1] != ' ')         { sureValues.push_back (Board [row+2][column+1]); }        if (Board [row+1][column+2] != ' ')        { sureValues.push_back(Board [row+1][column+2]); }        if (Board [row+2][column+2] != ' ')        { sureValues.push_back(Board [row+2][column+2]); }     }     if (column%3 == 1 && row%3 == 0)     {        if (Board [row+1][column-1] != ' ')        { sureValues.push_back(Board [row+1][column-1]); }        if (Board[row+2][column-1] != ' ')         { sureValues.push_back (Board[row+2][column-1]); }        if (Board [row+1][column+1] != ' ')        { sureValues.push_back(Board [row+1][column+1]); }        if (Board [row+2][column+1] != ' ')        { sureValues.push_back(Board [row+2][column+1]); }     }     if (column%3 == 2 && row%3 == 0)     {        if (Board [row+1][column-1] != ' ')        { sureValues.push_back(Board [row+1][column-1]); }        if (Board [row+2][column-1] != ' ')         { sureValues.push_back (Board [row+2][column-1]); }        if (Board [row+1][column-2] != ' ')        { sureValues.push_back(Board [row+1][column-2]); }        if (Board [row+2][column-2] != ' ')        { sureValues.push_back(Board [row+2][column-2]); }     }     if (column%3 == 0 && row%3 == 1)     {        if (Board [row-1][column+1] != ' ')        { sureValues.push_back(Board [row-1][column+1]); }        if (Board [row-1][column+2] != ' ')         { sureValues.push_back (Board [row-1][column+2]); }        if (Board [row+1][column+1] != ' ')        { sureValues.push_back(Board [row+1][column+1]); }        if (Board [row+1][column+2] != ' ')        { sureValues.push_back(Board [row+1][column+2]); }      }     if (column%3 == 1 && row%3 == 1)     {        if (Board [row-1][column-1] != ' ')        { sureValues.push_back(Board [row-1][column-1]); }        if (Board [row+1][column-1] != ' ')         { sureValues.push_back (Board[row+1][column-1]); }        if (Board [row-1][column+1] != ' ')        { sureValues.push_back(Board [row-1][column+1]); }        if (Board [row+1][column+1] != ' ')        { sureValues.push_back(Board [row+1][column+1]); }     }     if (column%3 == 2 && row%3 == 1)     {        if (Board [row-1][column-2] != ' ')        { sureValues.push_back(Board [row-1][column-2]); }        if (Board [row-1][column-1] != ' ')         { sureValues.push_back (Board [row-1][column-1]); }        if (Board [row+1][column-2] != ' ')        { sureValues.push_back(Board [row+1][column-2]); }        if (Board [row+1][column-1] != ' ')        { sureValues.push_back(Board [row+1][column-1]); }     }     if (column%3 == 0 && row%3 == 2)     {        if (Board [row-1][column+1] != ' ')        { sureValues.push_back(Board [row-1][column+1]); }        if (Board [row-2][column+1] != ' ')         { sureValues.push_back (Board [row-2][column+1]); }        if (Board [row-1][column+2] != ' ')        { sureValues.push_back(Board [row-1][column+2]); }        if (Board [row-2][column+2] != ' ')        { sureValues.push_back(Board [row-2][column+2]); }     }     if (column%3 == 1 && row%3 == 2)      {        if (Board [row-1][column-1] != ' ')        { sureValues.push_back(Board [row-1][column-1]); }        if (Board [row-2][column-1] != ' ')         { sureValues.push_back (Board [row-2][column-1]); }        if (Board [row-1][column+1] != ' ')        { sureValues.push_back(Board [row-1][column+1]); }        if (Board [row-2][column+1] != ' ')        { sureValues.push_back(Board [row-2][column+1]); }     }     if (column%3 == 2 && row%3 == 2)     {        if (Board [row-2][column-2] != ' ')        { sureValues.push_back(Board [row-2][column-2]); }        if (Board [row-1][column-2] != ' ')         { sureValues.push_back (Board [row-1][column-2]); }        if (Board [row-2][column-1] != ' ')        { sureValues.push_back(Board [row-2][column-1]); }        if (Board [row-1][column-1] != ' ')        { sureValues.push_back(Board [row-1][column-1]); }     }     return sureValues;}void CreateBoard(int Board[9][9]){    int row = 0;    int column = 0;    bool BoardStatus [9][9];      int BoardTemp;    Reset(Board, BoardStatus);    for (row = 0; row <= 8; row++)     {        for (column = 0; column <= 8; column++)        {            while (BoardStatus [row][column] == false)            {               if (Board [row][column] == -1)               {                 Board [row][column] = Random_Number();  // a random number is placed inside every cell before the validation process begins in order to randomize each puzzle.                 BoardTemp = Board [row][column];               }               BoardStatus [row][column] = Verify(Board, row, column);               if (BoardStatus [row][column] == false)               {                     Board [row][column] = Board[row][column]+ 1;                     if (Board [row][column] == 10) // A value inside a cell cannot be greater than 9.                     {                          Board[row][column] = 1;                     }                     if (BoardTemp == Board[row][column] )                     {                          Reset(Board, BoardStatus);                          row = 0;                          column = 0;                          }               }                        }        }    }}int Difficulty(){    cout << "Choose a difficulty among Easy, Intermediate, and Hard.";    string x;    int difficulty;    cin >> x;    for (int i =0; i <= x.size(); i++)    { x = toupper(x); }    if (x == "EASY" )    { difficulty = 0; }    if (x == "INTERMEDIATE" )     { difficulty = 1; }    if (x == "HARD" )    { difficulty = 2; }    return difficulty;}    void WriteFile(int Board[9][9]){     ofstream SudokuBoard;     SudokuBoard.open ("1.txt", fstream::app);     for (int row = 0; row <= 8; row++)     {          SudokuBoard << endl << "----------------------" << endl;          if ( row == 3 || row == 6)          {               SudokuBoard << "----------------------" << endl;          }          for (int column = 0; column <= 8; column++)          {              SudokuBoard << Board[row][column] << "|";  // prints horizontally.              if (column == 2 || column == 5)               {                    SudokuBoard << "| ";               }          }     }     SudokuBoard << endl  << endl;     SudokuBoard.close();}void Debug (int Board[9][9], bool BoardStatus[9][9], int row, int column, int BoardTemp){     Display(Board);     cout << endl;     cout << "column" <<  column << endl;     cout << "row" << row << endl;     cout << "BoardTemp" << BoardTemp << endl;     cout << BoardStatus[row][column] << endl;     system("pause");}void Display(int Board[9][9]){     for (int row = 0; row <= 8; row++)     {          cout << endl << "-----------------------------" << endl;          if ( row == 3 || row == 6)          {               cout << "-----------------------------" << endl;          }          for (int column = 0; column <= 8; column++)          {                cout << Board[row][column] << " |" ;  // prints horizontally.               if (column == 2 || column == 5)               {                    cout << "| ";               }          }     }}void Reset(int Board[9][9], bool BoardStatus[9][9]){    srand((unsigned)time(0));    for (int row = 0; row <= 8; row++)    {        for (int column = 0; column <= 8; column++)        {            Board [row][column] = -1;    //  Sets Board values to an impossible value to ensure that false positive comparisons are not made between cells.            BoardStatus [row][column] = false;        }    }}int Random_Number(){      int random_integer = (rand()%9)+1;      return random_integer;}bool Verify(int Board[9][9], int row, int column){     for (int n=0; n <= 8; n++)// If the given cell equals a cell in its horizontal row or vertical column, the cell's status is set to false.     {          if  (n != row && Board [row][column] == Board [n][column])          {               return false;          }          if (n != column && Board [row][column] == Board [row][n])           {              return false;          }     }    if (column%3 == 0 && row%3 == 0) // the following lines test whether or not the value in the cell equals a value within that cell's respective 3column3 square.     {        if (Board [row][column] == Board [row+1][column+1] || Board [row][column] == Board [row+2][column+1] || Board [row][column] == Board [row+1][column+2] || Board [row][column] == Board [row+2][column+2])        {           return false;           }     }     if (column%3 == 1 && row%3 == 0)     {        if (Board [row][column] == Board [row+1][column-1] || Board [row][column] == Board[row+2][column-1] || Board [row][column] == Board [row+1][column+1] || Board [row][column] == Board [row+2][column+1])        {           return false;        }     }     if (column%3 == 2 && row%3 == 0)     {        if (Board [row][column] == Board [row+1][column-1] || Board [row][column] == Board [row+2][column-1] || Board [row][column] == Board [row+1][column-2] || Board [row][column] == Board [row+2][column-2])        {           return false;        }     }     if (column%3 == 0 && row%3 == 1)     {        if (Board [row][column] == Board [row-1][column+1] || Board [row][column] == Board [row-1][column+2] || Board [row][column] == Board [row+1][column+1] || Board [row][column] == Board [row+1][column+2])        {           return false;        }     }     if (column%3 == 1 && row%3 == 1)     {        if (Board [row][column] == Board [row-1][column-1] || Board [row][column] == Board[row+1][column-1] || Board [row][column] == Board [row-1][column+1] || Board [row][column] == Board [row+1][column+1])        {           return false;        }     }     if (column%3 == 2 && row%3 == 1)     {        if (Board [row][column] == Board [row-1][column-2] || Board [row][column] == Board [row-1][column-1] || Board [row][column] == Board [row+1][column-2] || Board [row][column] == Board [row+1][column-1])        {           return false;        }     }     if (column%3 == 0 && row%3 == 2)     {        if (Board [row][column] == Board [row-1][column+1] || Board [row][column] == Board [row-2][column+1] || Board [row][column] == Board [row-1][column+2] || Board [row][column] == Board [row-2][column+2])        {           return false;        }     }     if (column%3 == 1 && row%3 == 2)      {        if (Board [row][column] == Board [row-1][column-1] || Board [row][column] == Board[row-2][column-1] || Board [row][column] == Board [row-1][column+1] || Board [row][column] == Board [row-2][column+1])        {           return false;        }     }     if (column%3 == 2 && row%3 == 2)     {        if (Board [row][column] == Board [row-2][column-2] || Board [row][column] == Board [row-1][column-2] || Board [row][column] == Board [row-2][column-1] || Board [row][column] == Board [row-1][column-1])        {           return false;        }     }     return true;}

##### Share on other sites
Your RecordValues() function returns a vector of ints, but it's declaration states that it should return an int. You should declare your function as following:
vector<int> RecordValues(const int Board[9][9], int row, int column);

Appending a vector to another vector can't be done with push_back. Try insert instead:
vector<int> sureValues;vector<int> recordValues = RecordValues(FinalBoard, i, theRandom);sureValues.insert(sureValues.end(), recordValues.begin(), recordValues.end());

However, in this case you might as well assign the result of RecordValues to sureValues immediately.

As for the rest of your program, personally I would use a few global vectors instead of passing around possibly large data sets like these. Globals are usually referred to as evil, but in a situation like this I'd simply make Board and sureValues global. Almost every function uses them already anyway and your program isn't very large, nor does it have too many different components or responsibilities.

Bytheway, Verify() and RecordValues() contain a lot of repetitive code. The main differences seem to be some values. Make these variables, so you can cut down on duplicated functionality.

And finally, a minor point: your naming conventions aren't consistent. Some variable names are lowercase, while others are CamelCase. Since function names are CamelCase here as well, that may cause some confusion. Whatever style you pick, be consistent - it'll make things easier. :)

##### Share on other sites
Quote:
 Original post by Captain PBytheway, Verify() and RecordValues() contain a lot of repetitive code. The main differences seem to be some values. Make these variables, so you can cut down on duplicated functionality.
I noticed the repetitiveness as well. I actually simply stole the code from Verify() to create RecordValues(). I originally attempted to create an algorithm to solve the problem for me, only to realize that I couldn't figure out how to design one. In the end, my solution worked quite nicely.

So what exactly are you suggesting that I make a variable? Do you mean all of the statements like Board[row+2][column+2]? Then everything I see this statement I can replace it with the variable? Or are you thinking of something else?

Quote:
 Original post by Captain PAppending a vector to another vector can't be done with push_back. Try insert instead:vector sureValues;vector recordValues = RecordValues(FinalBoard, i, theRandom);sureValues.insert(sureValues.end(), recordValues.begin(), recordValues.end());However, in this case you might as well assign the result of RecordValues to sureValues immediately.

How do I go about doing this "immediately?" I just took a peek at the standard library documentation for the insert function, but I am still not entirely clear how to do this. wait... does the code above ... this part... recordValues.begin(), recordValues.end()... take the beginning of the return and the end of the return of the recordValues function, or is it accessing the vector that you created moments earlier?

EDIT:
ahh... this is all you were looking for, I think:

vector<int> sureValues = RecordValues(FinalBoard, i, theRandom);

##### Share on other sites
Quote:
Original post by zoner7
Quote:
 Original post by Captain PBytheway, Verify() and RecordValues() contain a lot of repetitive code. The main differences seem to be some values. Make these variables, so you can cut down on duplicated functionality.
I noticed the repetitiveness as well. I actually simply stole the code from Verify() to create RecordValues(). I originally attempted to create an algorithm to solve the problem for me, only to realize that I couldn't figure out how to design one. In the end, my solution worked quite nicely.

So what exactly are you suggesting that I make a variable? Do you mean all of the statements like Board[row+2][column+2]? Then everything I see this statement I can replace it with the variable? Or are you thinking of something else?

If you take a close look at your Verify() function, you'll actually see two areas that can be compacted. The first is obvious: 9 nested if blocks. The only differences here are some values: 0, 1, 2, various combinations. Write that block once and throw a loop around it that generates those values.

The second repetition is less obvious: every if statement performs 4 checks. You managed to reduce that from 9 with the first part of the Verify() function, which is good, but you could still do better. Consider the following:

bool Verify(int Board[9][9], int row, int column){	for(int i = 0; i < 9; i++)		if((i != row && Board[row][column] == Board[column]) || (i != column && Board[row][column] == Board[row]))			return false;		for(int x = column % 3; x < (column % 3) + 3; x++)		for(int y = row % 3; y < (row % 3) + 3; y++)			if(x != column && y != row && Board[row][column] == Board[y][x])				return false;		return true;}

Please note that I didn't test this, it should merely serve as an example. Actually, I did try testing your code (you forgot to include <string> btw), but ran into a problem: your CreateBoard() function takes a large amount of time. That is, it's generating random boards, rejecting them, generating others, rejecting, etc. You may want to think about a more efficient approach.

Quote:
 ahh... this is all you were looking for, I think:vector sureValues = RecordValues(FinalBoard, i, theRandom);

Exactly. As for the insert function, it allows you to insert an object, or a range of objects, at a specific place. In the example I gave you, all elements from the second vector (from begin to end) would be inserted at the end of the first vector.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 12
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633723
• Total Posts
3013545
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!