how to define a bidimensional dynamic array as a global variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lumumba401
    New Member
    • Oct 2008
    • 1

    #1

    how to define a bidimensional dynamic array as a global variable?

    Hello everybody,

    i am asking about how to define a bidimensional dynamic array as a global variable to use as incoming variable in a function

    Let us see , for example in a part of a programm my problem:

    #include<iostre am>
    ...
    using namespace std;

    float a[10][10];//in this place i wish define a bidimenional dynamic array agian: how to do it?
    ...
    //follow the function

    float suma(int k, float a[][10])
    {
    float s=0;
    for (int i=1;i<k;i++)
    for (int j =1;j<k;j++)
    s=s+a[i][j];
    return (s);
    }

    int main()
    {
    ...
    suma(k,a);
    ...
    }

    But instead defining an static bidimensional array a[10][10], to use after as an incoming variable in a function, i wish to define it as dynamic array(to work with a huge array, for example(a[500][500]) )

    would you like to help me?
    Bye .
    Miguel.
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    First of all, it's usually not a good idea to use global variables. Then, before you start using 2D dynamic arrays, you should understand 1D dynamic arrays; you can then implement a 2D array as an array of an array. If performance is a real issue then use a proper matrix class like the one provided in boost or blitz++ (C++) or GSL (C/C++), there are many others.

    Also, if your problem is simply that you wish to work with large arrays then this has nothing to do with dynamic arrays. Just declare a large static array.

    Comment

    Working...