[prog] strcutures containing structures...
Tim King
timk at jtse.com
Mon May 28 16:12:08 UTC 2007
sena emre <senaemre at yahoo.com> wrote:
> struct box{
> int w;
> int h;
> int d;
> }:
>
> struct solution{
> int no;
> int quality;
> struct box Cargo[10];
> }
>
> solution B[20];
>
> for(int bc=0; bc<20; bc++)
> for(int f=0; f<10; f++)
> B[bc].Cargo[f].w = 10;
>
Hi, Sena. Yes, this should work correctly. Except that "w," "h," and "d"
should be "width," "height," and "depth." Likewise, "no," is the number
number of solution ("solution_num") or is it the number of cargoes in
the solution ("num_cargoes")? And "bc" and "f"... I dunno what they
stand for.
(BTW, this is C++, not C. I can tell because you declare your loop
variables inside the for-statement. You can't do that in C. ;-) )
So... Given these data structures, here is the kind of code I would
probably use to initialize them. I don't know how much a beginner you
are, so if this goes over your head, please forgive me. But I think you
can still get the gist of it.
struct box_t {
int width;
int height;
int depth;
};
extern const size_t NUM_CARGOES_PER_SOLUTION;
struct solution_t {
int num_cargoes;
int quality;
box_t cargos[10];
}
And then...
const size_t NUM_CARGOES_PER_SOLUTION = 10;
const size_t NUM_SOLUTIONS = 20;
solution_t solutions[NUM_SOLUTIONS];
void init_solutions(void) {
for (int idx_solution = 0; idx_solution < NUM_SOLUTIONS; ++
idx_solution) {
solution_t& solution = solutions[idx_solution];
solution.num_cargoes = 0;
solution.quality = 0;
for (int idx_cargo = 0; idx_cargo <
NUM_CARGOES_PER_SOLUTION; ++ idx_cargo) {
box_t box = solution.cargoes[idx_cargo];
box.width = box.height = box.depth = 0;
}
}
}
Hope this helps,
-TimK
--
http://LucrativeWebDesign.com/
http://www.JTimothyKing.com/blog/
More information about the Programming
mailing list