Spec interpretation

I don't actually need any coding help, however, in my coursework to create a sudoku solver, I have to create something "defined as an array of nine CellGroup
objects, linking to a total of 81 Cell objects" for the row, column and 'block'.

I am not sure how to do this? Maybe something like linked list or something? I don't know.
I think they mean linking in a more abstract sense, that you should be able to get to/access the 81 Cell objects somehow by using the array of 9 CellGroups. You don't want to use a linked list here. Arrays are all you need.
Oh ok, so would I create an array containing 9 cells? or 81?
I think they want an array of 9 'Cellgroup', and each 'Cellgroup' contains an array of 9 'Cell's.
The description says you should have an array of 9 CellGroups. Exactly how you represent a CellGroup is up to you. It could simply be an array (an array with 9 elements or possibly a 3x3 2D array), or maybe a struct/class object containing such an array.
Can you put arrays in arrays? Or am I just thinking this wrongly?
sure.
 
int board[9][3][3];

Here board is an array of 9 int[3][3].
How about something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
class Cell 
{  int            value;
    bool         fixed;
...
};
class CellGroup
{  Cell           cell[3][3];
...
};
class Board 
{  Cellgroup  cg[3][3];
...
};

I've created an array of pointers in a cellgroup class,
Cell *cellPointers[9];
I created 3 more arrays for the row, column and block.

So my row array, has 9 arrays, each with 9 pointers in it. My question is, I need to populate the board with a text file, but how do I put all the 81 numbers into my row arrays, if you understand what I mean.
I'm not sure why you need pointers to the cells. That creates memory mnagement issues and creates the potential for memory leaks. I think you will find it easier to treat the cellgroup as a 3x3 array of cells.

If you really want to use pointers, cellgroup's constructor will need to allocate 9 Cells and assign those pointers to the respective cellPointers.
Don't forget that cellgroup's destructor will need to delete those pointers.
We have to use pointers for the coursework, and okay thanks, i'll see if i can do anything haha,
Sorry for not understanding, but C++ confuses me.

I have 3 classes whcih are:
1
2
3
4
5
6
class Cellgroup
{

public:	
	Cell *cellPointers[9];
};

1
2
3
4
5
6
7
8
9
class Cell
{
public:
	std::vector<int> possibleCanidates;
	int value;
	bool cellFixed;

} board[9][9];

1
2
3
4
5
6
7
class Sudoku
{
public:
	Cellgroup row[9];
	Cellgroup column[9];
	Cellgroup block[3][3];
};


Am I suppose to be putting constructors anywhere? I guess I need to add some to the Cellgroup class, and probably the cell, but i'm not sure whatsoever.
an array(s) would be suffice, in my opinion :)
hmm, i think i am going to use arrays, but i dont know my next step.
Ahh, I see what you're doing with your Sudoku class. You have separate Cellgroups for the 9 rows, the 9 columns and the 9 blocks. I think that's making things more complicated. When you update a cell, you're going to have to update it in 3 places, actually 4 places if you include board[9][9]. I think it's much easier to keep one image of the state of the board, which is what I suggested in my earlier post.

Am I suppose to be putting constructors anywhere?

Yes, Cellgroup will need at a minimum constructor and destructor. You will probably also want a copy constructor and assignment operator. See:
http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)
I explained the constructor in my earlier post.

And yes, Cell should have a constructor to initialize value and cellFixed. Do not assume that because you're always initializing from a file, you can do this at the time you read each cell from the file. If you create a temporary cell anywhere (such as pushing onto a vector) you're going to want it initialized.
Last edited on
Topic archived. No new replies allowed.