Reputation: 329
I was asked to create a matrix with 5 rows and unknown column. And my boss want me to use a 1 dimensional buffer. concatenated by 5 rows buffer. I don't get what is that mean, can some one provide me a simple example please!
With array I can do
double[][] arr = new double[5][someNumber];
But he says then the size would be limited.
So I don't know what he means by using a DOUBLE buffer, I am not very good @C++
Thank you very much, an example would be nice!
Upvotes: 0
Views: 1168
Reputation: 503
Sounds like you're saying
double *arr[5];
for(unsigned int x = 0; x < 5; ++x)
{
arr[x] = new double[someNumber];
}
Since, you know that you have 5 for sure, and an unknown part my assumption is this is how you're referring to it.
Upvotes: 0
Reputation: 476990
For R
rows and C
columns declare double arr[R * C]
, and arr[i * C + j]
is the element at cell [i, j]
.
This generalizes to arbitrary dimensions.
Flattening out an array like that can be a very useful optimization, especially when you use dynamic arrays such as std::vector
, where you can get a single dynamic array rather than one for each row.
Upvotes: 1