Hello
I need a 2-dimensional dynamic bool array with 2 fixed columns and dynamic number of rows.
What would be the proper way of implementing it?Code:bool *name[2];
name = new bool..?
Thanks a lot for help!
Printable View
Hello
I need a 2-dimensional dynamic bool array with 2 fixed columns and dynamic number of rows.
What would be the proper way of implementing it?Code:bool *name[2];
name = new bool..?
Thanks a lot for help!
Apart from all the weird specialization stuff, what's wrong with vector< vector<bool> >?
Or vector<bool> name_col1, vector<bool> name_col2. Or vector<bool> name[2]?
Because I'm not allowed to use vector.
Do you ever have to resize? Or can you just say new bool[some_number][2] and be done with it?
Can you use std::pair? Then you could use a normal dynamic array of pairs.
I didn't realize at first you wanted fixed size columns instead of rows. I don't know the proper syntax off the top of my head. My guess is that it involves strategically placed parentheses.
Edit: tabstop's idea is probably best.
Isn't it just this?
(m and n are arbitrary)Code:name[0] = new bool[ m ];
name[1] = new bool[ n ];
What is wrong with using bool *name = new bool[ width * height]?
I dont have to resize it once its allocated.
Oh ok. Well how about this then:
Example:
I dunno if anyone had suggested that before, but it sounds good to me.Code:bool *name = new bool[width * height];
This will give me an error:
Code:bool **arr;
arr= new bool[x][2];
and then dynamically allocate the rows (name[][0] and name[][1]).Code:bool** name = new bool*[2];
Yep :) Behold!
Example:
Code:#include <iostream>
#define WIDTH 5
#define WIDTH 5
inline
bool &getValue(bool *name, size_t x, size_t y)
{
return name[y*WIDTH + x];
}
int main(void)
{
bool *name = new bool[WIDTH * HEIGHT];
// do stuff..
std::cout << getValue(name, 3, 4) << std::endl;
return 0;
}
I need that twodimensional bool array to write in all possible combinations of 0 and 1 without repeating.
So if I have a variable x = 3 (which means number of cells)
There are 3^2 (8) possible combinations.
Now the array should look like:
Any help on implementing this would be highly appreciated!Code:0: 0 0 0
1: 0 0 1
2: 0 1 0
3: 0 1 1
4: 1 0 0
5: 1 0 1
6: 1 1 0
7: 1 1 1
If I were given so many arbitrary restrictions like you have in your assignment. I would write my own custom container class. But I guess I am full of crazy ideas like that---or perhaps I understand that the whole objective of the assignment is to get you to realize that is the best option.
I'm probably being stupid, but why do you need a 2D array for this?
I also think that tabstop has given the answer. Use bool (*table)[2] = new bool [somenumber][2]
Or just use a fixed size array if you can be reasonably sure that x isn't going to be enormous - there is no great difference in how much memory you can allocate for a dynamic array or a fixed array, and until you actually USE the mmemoy in a fixed array, it won't actually use up (much) RAM in your machine.
--
Mats
This is not a two-dimensional array with exactly two columns. This is a two-dimensional array where the columns (n) and rows (2**n) are both dynamic. Is it a requirement to use a two-dimensional array to solve this problem? It's pretty easy to get from 7 to 1 1 1 without it.
You can make this prettier with a well-placed typedef.Quote:
I also think that tabstop has given the answer. Use bool (*table)[2] = new bool [somenumber][2]
Code:typedef bool (*TwoBools)[2];
TwoBools *table = new TwoBools[somenumber]