简体   繁体   中英

Assigning Defined elements to a 2D Array C

I have a 3 large groups of integers that I would like to add to different rows of an array. These integers are defined as follows:

#define APARTMENT1_USAGES {0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.111, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.065, 0.167, 0, 0, 0, 0.048, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0}
    #define APARTMENT2_USAGES {0, 0, 0, 0, 0, 0, 0, 0.130, 0, 0, 0, 0, 0, 0.176, 0, 0.125, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.161, 0.000, 0.039, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0.000, 0.109, 0, 0.032, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.152, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0.135, 0, 0, 0, 0, 0, 0, 0, 0, 0.100, 0, 0, 0.063, 0, 0, 0, 0, 0.000, 0, 0.025, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0.000, 0, 0, 0, 0, 0, 0.378, 0, 0.147, 0.229}
    #define APARTMENT3_USAGES {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.048, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0.01, 0.01, 0.02, 0.03, 0.03, 0, 0, 0, 0, 0.088, 0, 0}

I have shortened these just to show the idea, they are usually 30x24 elements. What I am trying to do is load each of these into a row of a 2D array, but am getting a syntax error without an explanation. I have tried it as follows:

double apartmentUsage[3][30*24];
apartmentUsage[1][30*24] = APARTMENT1_USAGES;
apartmentUsage[2][30*24] = APARTMENT2_USAGES;
apartmentUsage[3][30*24] = APARTMENT3_USAGES;

One of the errors you are facing is as follows: If you define array[3] , you can have three values stored in array[0] , array[1] and array[2] . In your code. its defined as apartmentUsage[3][..] but starts with 1 . Should be like this-

int multiply=30*24;
apartmentUsage[0][multiply] = APARTMENT1_USAGES;
apartmentUsage[1][multiply] = APARTMENT2_USAGES;
apartmentUsage[2][multiply] = APARTMENT3_USAGES;

**BUT** this too won't completely solve your problem. Try doing it in the initialization itself as pointed in the comments - double apartmentUsage[3][multiply] = {APARTMENT1_USAGES, APARTMENT2_USAGES, APARTMENT3_USAGES};

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