Question subject: dynamic array as global variables
Posted: Sat Nov 08, 2008 3:25 pm
Joined: Sun Oct 19, 2008 3:47 pm Posts: 281 Has thanked: 0 time Have thanks: 1 time
well i wish to ennounce two global variables to use after in a function. One variable i define as an unidimensional vector. I did it very well, i think so, the variable is called x[]. But i wish to define a new global variable, as bidimensional dynamic array, is called a[][]. But i don't know how to define this dynamic array a[][] as global variable.
I knwo how to define dynamica array(unidimensional or bidimensional) inside the main program or inside any function, but as a global variable?
It is important to remark that i have defined in my program three or more functions whose one of incoming variables is a bidimeniosnal dynamic array.
Next i post a part of program
Program :
Code:
using namespace std; float a[16][16]; //in this part i wish define the bidimensional dynamic array
//i'm defining unidimensional dynamic array x const N=1000; float* x = new float[N]; //first function using dynamic array x[]
float suma3(float x[]) { int j; float s=0.0; for (j=1;j<16;j++) s=s+x[j]; return(s); } //second function i wish , for example here, usin a[][] as bidimensional dynamic array. how to do? float suma1(int i, float x[], float a[][16]) { int l; float s=0; for (l=1;l<i;l++) { s=s+a[i][l]*x[l]; } return(s); }///fin suma 1
AnswerBot
Question subject: Re: dynamic array as global variables
Posted: Sat Nov 08, 2008 3:27 pm
Joined: Sun Oct 19, 2008 3:53 pm Posts: 229 Has thanked: 0 time Have thanks: 0 time
they can be defined before headers, before using namespace, and it's the same for all variables. Something you might try is:
Code:
float* vals; int main() { vals=new float[i][j]; }
And it is before your loop. you can't put code out side of a function like that.