Here's the scenario:
In a class, I have a two dimensional array, let's call it: 2DArray.
To declare it, I use:
Now, naturally, it is an array of integers, holding 441 (21 x 21) values.
But, this "20" I use is pretty open to change. Sometimes I might feel like throwing in a 50, sometimes a 2, it depends on my current mood. Now, the problem is, I use this number "20" many many times throughout my class AND main program, so, going through each time and changing the "20"s that apply can get rather tiresome.
What I was hoping was to find a way that I could make a variable within my class, to hold the dimensions of my 2DArray (it will always be symmetrical, I.e. 1x1 or 5x5 or 50x50), and then declare a 2D Array based on that variable? That way I'll also be able to retrieve the value from my main app and use the number there if calculations are needed.
Umm... Actually, it only holds 400 (20x20) values.
At any rate, to solve your problem, I recommend one of the following:
1) Use dynamic memory allocation; just be sure to clean up properly when you resize, which would be a pain to deal with.
2) Use a constant for your values: 'const int ARRAY_SIZE = 20;' somewhere in your code, and then use ARRAY_SIZE where you had used your literal value (only requiring one change and then a recompile).
3) Use STL and a vector inside a vector: i.e. 'vector<vector<int> > m2DArray'.
The best is the last suggestion. The first is the most dangerous if you are not very familiar/comfortable with dynamic memory allocation.
Bah, you're right,  = [0 to 19], not [0 to 20] :)
And as for those three options...I have no clue about anything that Options 1 or 3 suggest :) But I'm liking the simplicity (and distance from messing with memory) that number 3 has, but I have no clue what STL is, or how I could use vectors for it...but I really don't want to go messing with memory allocation.
I'll give #3 a shot and I'll most likely be back here in the near future with a couple of questions :) Thanks
Good call staying away from #1. As for #3, try this thread for some pointers to STL info: http://cboard.cprogramming.com/showthread.php?t=53919
Vectors are actually quite nice. You can treat them just like arrays, and they are resizable (with their own internal memory management). The one thing I should warn you of is the inefficiency of resizing an array/vector in such a manner, though perhaps this is not much of an issue. Just make sure that all of your vectors get resized.
A fourth idea (which is likely overkill) is to find a library with a good matrix class.
For simplicity, at the expense of some memory, you could do
const int MAX_SIZE = 30;
typedef int twoDArray[MAX_SIZE][MAX_SIZE];
Sure it wastes some memory if all you want is a 5x5, but so long as the max isn't too big, and you don't have too many of them (like hundreds), then it's a pretty easy way to deal with the problem.
void foo ( twoDArray bar, size_t current_size );
Pass the array, and whatever it's current size is (up to the max), and everything takes care of itself. :)