How many elements are in the following array? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
- 4

How many elements are in the following array?

int arr[0][2][4][3];

3rd Apr 2018, 10:47 AM
Prafulla Kumar
13 Réponses
+ 8
Jay Matthews Indeed... zero based indexes apply when accessing a value from a specific array element. However, when declaring an array, the numbers between the brackets refer to the size of the array, not position index. For example, the following line declares an int array with a fixed size of 3 element. int arr[3]; // Declared with a fixed size of 3. Below is a list all possible variations for accessing values using zero-based index: arr[0] = 5; // 1st position at index 0 arr[1] = 10; // 2nd position at index 1 arr[2] = 20; // 3rd position at index 2
4th Apr 2018, 2:57 AM
David Carroll
David Carroll - avatar
+ 6
J.G. Since the type int is specified, it would be a declaration.
4th Apr 2018, 12:49 AM
David Carroll
David Carroll - avatar
+ 5
0 elements - assuming you can declare an array with 0 elements in the 1st dimension, or any dimension for that matter. 0*2*4*3 == 0 If the first dimension is initialized with 0 elements, then the 2nd dimension will never be exist because it's containing element does not exist. This logic subsequently applies to the 3rd and 4th dimensions.
4th Apr 2018, 12:56 AM
David Carroll
David Carroll - avatar
+ 4
David Carroll Ah! I see now! I didn't catch that before.
4th Apr 2018, 11:39 AM
J.G.
J.G. - avatar
+ 3
arr[0] = 1 arr[2] = 3 arr[4] = 5 arr[3] = 4 Ans: 1*3*5*4= 60
3rd Apr 2018, 11:02 AM
✌️ RK ✌️
✌️ RK ✌️ - avatar
+ 2
60
3rd Apr 2018, 10:52 AM
Prafulla Kumar
+ 2
Ummm no? In C++ a zero sized array isn't allowed. Even if some compilers allow it the resulting array size is 0. ( sizeof(arr) / sizeof(int) ) Creating a zero sized array through new[] is allowed though but derefencing it is undefined.
3rd Apr 2018, 12:18 PM
Dennis
Dennis - avatar
+ 2
Dennis Where did the size 0 array come from? I don't think that is what is being talked about here
3rd Apr 2018, 12:20 PM
J.G.
J.G. - avatar
+ 2
Dennis Maybe I'm misinterpreting, but I don't believe that is an array declaration. i think that is supposed to be like an access to the last element of the array. maybe I'm wrong
4th Apr 2018, 12:13 AM
J.G.
J.G. - avatar
+ 1
4 elements
3rd Apr 2018, 2:51 PM
Pankaj singh
Pankaj singh - avatar
0
@J.G. In the question it says arr[0][2][4][3]? The 0 makes it illegal. And even if the 0 was a 1, or if the array was changed to arr[2][4][3], the size would still be 24, not 60.
3rd Apr 2018, 12:23 PM
Dennis
Dennis - avatar
0
Pankaj singh It's a 4D array, not a 1D array initialized to 0, 2, 4, 3.
3rd Apr 2018, 3:00 PM
Dennis
Dennis - avatar
- 1
index of arr[0]=0 index of arr[1]=2 index of arr[2]=4 index of arr[3]=3
3rd Apr 2018, 2:55 PM
Pankaj singh
Pankaj singh - avatar