M
Mark S.
Hi,
even though I'm still at the beginning of my learning process, I am
anxious to program something "real". So I would like to write a little
program to solve Sudokus with variable dimensions (n*n). I think I have
a pretty good idea about the algorithm, but since you guys here strongly
suggested to avoid new & delete as much as possible, I was wondering how
you would suggest how I store the classes:
Basically, I think I need a class to store the dimensions of the field
which points to the n*m elements (with their own class). I thought I
would do something like this:
class Element {
public:
Element(int i = 0); // 0 means no number
// more variables to store status, etc.
}
class Field {
/* I don't know how to get any of these to work */
vector<Element> e || Element e[][] || Element* e // ???
public:
Field(int x = 1);
// functions to solve the Sudoku
};
The problem is that I don't know how to deal with multidimensional
vectors or arrays. I was thinking of using a single vector which is n*n
big. Of course, I would have to convert the numbers then (eg. for 3*3,
[1][1] would become [3]). Moreover, this solution does not seem very
elegant to me.
So, what would you recommend? An array of pointers to the elements? A
vector of pointers? Or a vector of the elements themselves?
Thank you in advance!
Mark
P.S.: I have used Google, of course, but I did not get any of these to work:
*[1]* Array (uses 'new' so maybe not recommended?):
This works in main():
const int n = 3;
int (*np)[n] = new int[n][n];
np[2][3] = 4;
However, I don't get it to work in my class (I know that the syntax is
wrong, but I don't know what the correct one is):
class Field {
const int n; // field dimensions
int x, y; // subsection dimensions
int (*fp)[];
public:
Field(int x = 1);
};
Field::Field(int i) : n(i) {
// { calculate subsection }
fp = new int[n][n];
}
*[2]* Vector:
I only know it's something like
vector< vector<Element> > f;
But I wouldn't even know how to push The elements to the right row, let
alone how to do this through a class.
even though I'm still at the beginning of my learning process, I am
anxious to program something "real". So I would like to write a little
program to solve Sudokus with variable dimensions (n*n). I think I have
a pretty good idea about the algorithm, but since you guys here strongly
suggested to avoid new & delete as much as possible, I was wondering how
you would suggest how I store the classes:
Basically, I think I need a class to store the dimensions of the field
which points to the n*m elements (with their own class). I thought I
would do something like this:
class Element {
public:
Element(int i = 0); // 0 means no number
// more variables to store status, etc.
}
class Field {
/* I don't know how to get any of these to work */
vector<Element> e || Element e[][] || Element* e // ???
public:
Field(int x = 1);
// functions to solve the Sudoku
};
The problem is that I don't know how to deal with multidimensional
vectors or arrays. I was thinking of using a single vector which is n*n
big. Of course, I would have to convert the numbers then (eg. for 3*3,
[1][1] would become [3]). Moreover, this solution does not seem very
elegant to me.
So, what would you recommend? An array of pointers to the elements? A
vector of pointers? Or a vector of the elements themselves?
Thank you in advance!
Mark
P.S.: I have used Google, of course, but I did not get any of these to work:
*[1]* Array (uses 'new' so maybe not recommended?):
This works in main():
const int n = 3;
int (*np)[n] = new int[n][n];
np[2][3] = 4;
However, I don't get it to work in my class (I know that the syntax is
wrong, but I don't know what the correct one is):
class Field {
const int n; // field dimensions
int x, y; // subsection dimensions
int (*fp)[];
public:
Field(int x = 1);
};
Field::Field(int i) : n(i) {
// { calculate subsection }
fp = new int[n][n];
}
*[2]* Vector:
I only know it's something like
vector< vector<Element> > f;
But I wouldn't even know how to push The elements to the right row, let
alone how to do this through a class.