0
Why 14, cant understand why [2][3] in here
int a[2][3]={1,2,3,4,5}; int i=0,j=0; for(i=0;i<2;i++) cout<<a[i][j];
7 Réponses
+ 8
int a[2][3] = {1, 2, 3, 4, 5};
This initialised the multi-dimensional array as
{ {1, 2, 3}, {4, 5} }
We have i = 0 and j = 0 initially.
The for loop iterates such that the program will print a[0][0] and a[1][0].
a[0][0] refers to the first element of the first dimension, which is 1.
a[1][0] refers to the first element of the second dimension, which is 4.
// outputs 14
+ 8
Think of cookies, e.g.
When we do int a[2][3], we have two packets, and each packet has 3 cookies
🍪🍪🍪 <- packet 0
🍪🍪🍪 <- packet 1
When we say a[0][0], we refer to the first cookie in packet 0.
When we say a[1][0], we refer to the first cookie in packet 1.
+ 6
[2][3] is more like
{ {x,x,x}, {x,x,x} }
It consists of 6 slots in total, but only five are initialized according to the question, so the last element may contain undefined values.
0
ohh thanks, i cant understand cause of 2/3, i cant understand clearly why
0
yeah, so if 2/3, thats like {{x,x},{x,x},{x,x}}
0
so 6 numbers, but not 5
0
so thats why i cant understand this task