简体   繁体   中英

C: putting arrays in arrays properly

myRainfallDB is a three-dimensional array containing information about the rainfall in random places on earth. I want to set up the following array structure:

myRainfallDB[] contains a list of place records. These coordinates must be stored in doubles.

myRainfallDB[][] contains

  • At index 0: a double containing the X coordinate of the place
  • At index 1: a double containing the Y coordinate of the place
  • At index 2: an array containing twelve doubles storing the amount of rainfall every month

I know I could probably get things done more efficiently by using classes, but I'm just learning C so I only want to toy with arrays for now. How would I go about declaring this 3D array?

You need a structure or simply put a user-defined data structure. Something like:

struct rainfall_t {
   double x, y; /* location */
   double monthly_rainfall[ 12 ];
};

In C, arrays are homogeneous ie they can store data of a single type only. Even for multi-dimensional arrays. You possibly cannot have an array for all the data you describe without first wrapping it up in a struct .

You could then create an array of rainfall_t and use it as you wish:

struct rainfaill_t db[ 100 ]; /* a DB of 100 locations */

You should also look up dynamically allocated memory in case you do not know up front how many locations you will have to deal with.

I think more than array you would first need a structure to store the information you have mentioned. You require an array of structure than a 3 dimenensional array.

struct RainfallDB
{
double XCord;
double YCord;
double Mnths[12];
};

Now in the function you can declare an array like this.

struct RainFallDB raininfo[100];

This will store the information of 100 records. If you dont know how many records user would like to store you can create a linklist out of the structure and dynamically allocate using malloc.

Hope this helps.

You really don't want to use a 3D array for this - C is not good at dealing with multi-dimensional arrays. Also, it is much easier to deal with names rather than array indexes, so instead, create a struct:

typedef struct {
   double x, y;
   double rainfall[12];
} places;

and then create a 1-dimensional array of them:

places p[100];   // 100 places

Yes, use structs for the job

struct myRainfallRecord {
     double x;
     double y;
     double **monthlyrainfall;
}

and have an array of myRainfallRecords. Monthlyrainfall can be malloc'd using malloc(12*sizeof(double)) when you fill in the value of the struct.

In a multidimensional array the higher dimensions are composed of lower-dimensioned arrays.

So, only your final array will contain anything other than an array.

It seems like you don't really need that third dimension, right?

So:

rfd[][14] = { 
                   { X_coord, Y_coord, rain, rain, rain, ... },
                   { X_coord, Y_coord, rain, rain, rain, ... },
                   { X_coord, Y_coord, rain, rain, rain, ... },
};

You will need a parallel array for the place names. This is why one would normally use a structure for this type of data.

char *places[] = { "Spain, the plains", ... };

(OR, struct Rain { char *place, double x,y; double rain[12]; }; )

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