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];

3rd Aug 2017, 10:14 AM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar
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
3rd Aug 2017, 11:02 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
3rd Aug 2017, 12:31 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
3rd Aug 2017, 12:50 PM
Hatsy Rei
Hatsy Rei - avatar
0
ohh thanks, i cant understand cause of 2/3, i cant understand clearly why
3rd Aug 2017, 11:45 AM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar
0
yeah, so if 2/3, thats like {{x,x},{x,x},{x,x}}
3rd Aug 2017, 12:35 PM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar
0
so 6 numbers, but not 5
3rd Aug 2017, 12:35 PM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar
0
so thats why i cant understand this task
3rd Aug 2017, 12:53 PM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar