C++ Global variable and function -


i programming in c++ using different functions. have problem declaration , usage of local , global variables. there many variables first have used in main, , need used in functions.

so, declared global variables. want allocate memory outside main().

but code not seem work. please me solve issue.

i have:

int *mb = new int [c];  //declare variables before use them matrix *m = new matrix  [n+1+m]; 

when use above 2 variables global variables code not work.

here part of code:

int p=4, n=5, m=5; int num; struct matrix{ public: int row; int col; int val; };  int p(int k){ return k-1; }  void m_to_c(){  int *mb,*mc,*mi; double *mv; int i, cur_col; matrix*m; cur_col=-1; for(i=0; i<= num; i++) {     cur_col=m[i].col;     mb[cur_col]=i;     mc[cur_col]=1;     mi[i]=m[i].row;     mv[i]=m[i].val; }}  int main (int argc, char **argv){ int status = 0;  int project = 4;  int employee = 5; int time = 5;   int empl [] = {2, 2, 2, 3}; int meet [] = {2, 4, 3, 3};  int numcols=n+m;  double *lb = new double [numcols]; double *ub = new double [numcols]; double *x  = new double [numcols]; int    *mb= new int [numcols]; int    *mc= new int [numcols]; char   *ctype = new char [numcols];  int **f = new int*[employee];  for(int = 1; <= employee; a++){          f[a] = new int [time];     for(int b = 1; b <= time; b++){         f[a][b]=rand()%2;     }}  int numrows=0;    for(int i=1; i<=n; i++){             for(int j=1; j<=m; j++){          for(int l=1; l<=p; l++) {             if(f[i][j] ==0){                             numrows++;                                     num++;             }         }      } }  int    *mi= new int [2*numrows+m]; double *mv= new double [2*numrows+m]; matrix_entry *m = new matrix_entry  [num+1+m];  num=-1; int row=-1; for(int i=1; <= n; i++) {     for(int j=1; j <= m; j++) {         for(int k=1; k <= project; k++) {             if(f[i][j] ==0) {                 row++;                 num++;                 m[num].col=p(k);                  m[num].row=row;                 m[num].val=-1;             }}}}  m_to_c(); 

you can define them globals , populate them in main thus:

int *mb; matrix *m;  int main () {     mb = new int [c];      m = new matrix  [n+1+m];     : } 

but should warn global variables (and short variable names mb, lb , ub) considered bad idea.

you better of limiting accessibility placing them in class or passing them around references, needed.


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -