简体   繁体   中英

how to use an array like a size or dimension in another array of 2D in C++

i have a set of Jobs J= 10, and h is the index of J, Each job h is executed exactly in one mode m in M(h) where m stands for the number of workers assigned to the job h and is related to a processing time Pjob(h,m).

can anybody help me and fix it and how to use M(h) in For loop

So this my code of declaration:

#include <iostream> 
using namespace std;

#define J 10   
int h; // index of Job J 
int m; // index of Mode M

int M[J] = { 2, 2 , 3 , 4 , 5 , 1 , 3, 3 , 2 , 1 };// index m
int Pjob[J][M[J]];// processing time for job h performed in mode m 

so it shows me an errors and i thought that the declaration was fault:

Error (active) E0028 expression must have constant value Error C2131 expression was not evaluated to constant Error C2148 total array size must not exceed 0x7ffffffff bytes

can anybody help me and fix it and how to use M(h) in For loop

Arrays must have regular dimensions (meaning, they must be square, or cubic, etc). Just declare your Pjob array to have the largest regular size:

#define J 10
#define MAX_MJ 5
int M[J] = { 2, 2 , 3 , 4 , 5 , 1 , 3, 3 , 2 , 1 };// index m
int Pjob[J][MAX_MJ];// processing time for job h performed in mode m 

Then use it with your special indexing:

assert(job < J);
assert(mode < M[job]);
quux( Pjob[job][mode] );

This wastes space for unused elements, but for such a small object it is unlikely to be a problem.

The other option is to use a std::vector or other library container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM