I am programming in C++ with using different functions. I have a problem with the declaration and usage of local and global variables. There are many variables which first have to used in main, and also need to be used in the functions.
SO, I declared some global variables. I also want to allocate some memory outside main()
.
But now the code does not seem to work. Please help me solve this issue.
I have:
int *mb = new int [c]; //declare variables before you use them
matrix *M = new matrix [n+1+m];
When I use the above two variables as global variables then my code does not work.
Here is a part of my 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 a = 1; a <= 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; i <= 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();