0
cant understand this code,,,plx help me
#include <stdio.h> typedef struct { int h; int w; int l; } box; int main() { box boxes[3] = {{2, 6, 8}, {4, 6, 6}, {2, 6, 9}}; int k, volume; for (k = 0; k < 3; k++) { volume = boxes[k].h*boxes[k].w*boxes[k].l; printf("box %d volume %d\n", k, volume); } return 0; }
5 ответов
+ 1
Okay.
Here,
in this line,
box boxes[3] = {{2, 6, 8}, {4, 6, 6}, {2, 6, 9}};
is similar to:
boxes[0]={2, 6, 8}
boxes[1]={4, 6, 6}
boxes[2]={2, 6, 9}
Note: here you cannot write like this directly in your code .I wrote this just for your understanding.
here with boxes[0]={2,6,8} means you are inserting value 2 to boxes[0].h ,
6 to boxes[0].w and
8 to boxes[0].l
Then,
with boxes[1]={4,6,6} means you are inserting value 4 to boxes[1].h ,
6 to boxes[1].w and
6 to boxes[1].l
and
with boxes[2]={2,6,9} means you are inserting value 2 to boxes[2].h ,
6 to boxes[2].w and
9 to boxes[2].l
So now,
boxes[0].h=2
boxes[0].w=6
boxes[0].l=8
boxes[1].h=4
boxes[1].w=6
boxes[1].l=6
boxes[2].h=2
boxes[2].w=6
boxes[2].l=9
next in the for loop ,
it's just printing the multiplication of w,l and h in the for loop.
1st time:
k=0
and volume =boxes[0].h*boxes[0].w*boxes[0]=2*6*8=96
so it will print ,
box 0 volume 96
2nd time:
k=1
and volume =boxes[1].h*boxes[1].w*boxes[1]=4*6*6=144
so it will print ,
box 1 volume 144
3rd time:
k=2
and volume =boxes[0].h*boxes[0].w*boxes[0]=2*6*9=108
so it will print ,
box 2 volume 108
and then the loop will stop.
Hope you understood.
+ 2
The future is now thanks to science thanks 👏👏👏
+ 1
the box structure can be initialized with
box x={hValue, wValue, lValue};
so since in your case it is a 3 box Array it is used {box0value, box1value, box2value}. the three values of the three boxes are used to calculate the volume of each box.
2 * 6 * 8
4 * 6 * 6
2 * 6 * 9
+ 1
I just understand this right the sec I posted this 😅 as I didn't look at the whole code..
+ 1
BTW thanks a lot for explanation 👍👍👏