English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

The problem I have is that I want to create a 2D array that has a variable size. I see that I can not do the following:

int m, n;
double testarray[m][n];

as c++ will not let me declare variables for the size. How would I get around this? Perhaps you could write a little code for me to illistrate this?

Thank you,

Brian

2006-08-03 05:58:14 · 4 answers · asked by Brian D 1 in Computers & Internet Programming & Design

4 answers

There is several otion easiest one is using double pointer though many people are not comfortable with pointer.
Using double pointer,
double ** testarray;
testarray= new double* [m];
for(i=0;i testarray[i]= new double [n];

now you can use this test array as before.

2006-08-03 06:26:09 · answer #1 · answered by Tanaeem 4 · 0 0

You're only permitted to use variable value for the left-most array dimension ... this is because indexes into the array for the other dimensions need to be constant to calculate where in the array to begin the offset.

But you can simply allocate space for a one-dimensional array of the size necessary for the two-dimensional array (i.e. double testarray[m * n]) and then access elements as :

testarray[(X * n) + Y)] ... where X and Y are the row/column or column/row of the element you are seeking.

This can be expanded to work for any number of dimensions.

2006-08-03 06:57:22 · answer #2 · answered by Arkangyle 4 · 0 0

The Boost project (http://www.boost.org/) provides free peer-reviewed portable C++ source libraries. Many of these are candidates for inclusion in a future revision of the C++ language standard. In fact, the Boost project was started by members of the C++ standards committee's library subcommittee, to develop libraries that everyone agreed were needed in C++ but weren't ready at the time the C++ standard was released.

Boost libraries are reviewed, thoroughly tested, and documented. They are in wide use and are portable to nearly all platforms that run C++.

One of the Boost libraries is the Multidimensional Array Library
(Boost.MultiArray). From the docs:

"The Boost Multidimensional Array Library provides a class template for multidimensional arrays, as well as semantically equivalent adaptors for arrays of contiguous data. ... Boost MultiArray is a more efficient and convenient way to express N-dimensional arrays than existing alternatives (especially the std::vector> formulation of N-dimensional arrays). The arrays provided by the library may be accessed using the familiar syntax of native C++ arrays. Additional features, such as resizing, reshaping, and creating views are available..."

2006-08-03 07:16:53 · answer #3 · answered by logician1989 2 · 0 0

int substantial (void) { opt for the flow d, s1, s2; int *my_array; my_array = new int[50]; //or maybe 'new int[MAX_LENGTH] my_array[33] = 304; printf("thirty third position includes %d",my_array[33]); go back 0; }

2016-11-28 00:09:30 · answer #4 · answered by ? 4 · 0 0

fedest.com, questions and answers