help me
-
dear all any problem occurred when the following errers happened? : error C2057: expected constant expression thanks
Li Zhiyuan 5/10/2006
Well, the error message is clear enough no ? It says that somewhere the compiler expects a constant variable but you didn't provide it. And without any piece of code, it is impossible to tell more.
Cédric Moonen Software developer
Charting control [v1.2] -
dear all any problem occurred when the following errers happened? : error C2057: expected constant expression thanks
Li Zhiyuan 5/10/2006
-
dear all any problem occurred when the following errers happened? : error C2057: expected constant expression thanks
Li Zhiyuan 5/10/2006
Is this question related to your previous question?
-
Is this question related to your previous question?
no, this is what i just studied for dynamic multiple array...the code is here.... nt n,m; double c[n][m] = NULL; double * a[] = NULL; double * b[] = NULL; cin >> n; cin>>m; b[] = new double[m]; a[] = new double[n]; for (int i=0; i>>n; i++) { a[i] =(double) rand()/RAND_MAX; } for (int j=0; j{ a[j] =(double) rand()/RAND_MAX; } for (int k=0;k for (int kk=0;kk { if (k==0) c[k][kk] = a[k]; else c[k][kk] = b[kk]; } cout <<"\n"; delete [] a; a = NULL; } this is what i created a 2 D dynamic array...but the problem is there...how to create a 2D dynamic array? thanks
Li Zhiyuan 5/10/2006
-
no, this is what i just studied for dynamic multiple array...the code is here.... nt n,m; double c[n][m] = NULL; double * a[] = NULL; double * b[] = NULL; cin >> n; cin>>m; b[] = new double[m]; a[] = new double[n]; for (int i=0; i>>n; i++) { a[i] =(double) rand()/RAND_MAX; } for (int j=0; j{ a[j] =(double) rand()/RAND_MAX; } for (int k=0;k for (int kk=0;kk { if (k==0) c[k][kk] = a[k]; else c[k][kk] = b[kk]; } cout <<"\n"; delete [] a; a = NULL; } this is what i created a 2 D dynamic array...but the problem is there...how to create a 2D dynamic array? thanks
Li Zhiyuan 5/10/2006
li zhiyuan wrote:
how to create a 2D dynamic array?
Simple answer... You can't. Which is why you're getting an error. Longer answer... You cheat. Create a one dimensional dynamic array. But make it an array of
double *
s, then create those arrays.typedef double *pdouble;
// create a 2D virtual array
pdouble *c = new pdouble [N];
for (n = 0; n < N; n++)
c [n] = new double [m];.... ; do your processing code
// Tidy up 2D virtual array
for (n = 0; n < N; n++)
delete [] c [n];
delete [] c;Notice I'm using
delete []
instead ofdelete
. The [] means it will call the destructor for each member of the array(s). For simple types like double and double *, it makes no difference. But it makes no harm, and is a good habit to get into. Then you can make arrays of classes/structs that *could* have destructors, and you'll be thankful for your good habits. I hope that helped, Iain. ps, In addition, please use the <pre> / </pre> keywords in your posts. And longer variables. They're hard to read now, and you're going to hate yourself in two years time. -
li zhiyuan wrote:
how to create a 2D dynamic array?
Simple answer... You can't. Which is why you're getting an error. Longer answer... You cheat. Create a one dimensional dynamic array. But make it an array of
double *
s, then create those arrays.typedef double *pdouble;
// create a 2D virtual array
pdouble *c = new pdouble [N];
for (n = 0; n < N; n++)
c [n] = new double [m];.... ; do your processing code
// Tidy up 2D virtual array
for (n = 0; n < N; n++)
delete [] c [n];
delete [] c;Notice I'm using
delete []
instead ofdelete
. The [] means it will call the destructor for each member of the array(s). For simple types like double and double *, it makes no difference. But it makes no harm, and is a good habit to get into. Then you can make arrays of classes/structs that *could* have destructors, and you'll be thankful for your good habits. I hope that helped, Iain. ps, In addition, please use the <pre> / </pre> keywords in your posts. And longer variables. They're hard to read now, and you're going to hate yourself in two years time. -
thanks a lot friend, could you provide me with complete code creating 2 D dynamic array? thanks again
Li Zhiyuan
I already did provide you with complete code for creating a 2d array, and then destroying it. Look at the post you replied to.
-
thanks a lot friend, could you provide me with complete code creating 2 D dynamic array? thanks again
Li Zhiyuan
Please remove your blinders. :rolleyes:
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne