27th May 2019, 8:41 PM
Josiah Mitchell
Josiah Mitchell - avatar
6 ответов
+ 6
int arr [] = {1,2,3,4}; This puts numbers into your array at index 0 to 3 (arrays start counting at 0). int x = arr[0]; This stores the number in x that is at index 0 of that array. So x is 1 now. cout << arr[x]; You are accessing arr[1] here, the second element, which is 2.
27th May 2019, 9:02 PM
HonFu
HonFu - avatar
+ 5
First of all, index of elements starts from 0 in array. If you set vatiable x to ELEMENT (in other case, that was number 1), expected is that cout << arr[x]; became arr[1] and second index in arr are 2. So, arr[x] would be 2. Remember: 1 - index 0, 2 - index 1 and so on.
27th May 2019, 10:01 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 4
Hello Josiah Mitchell, let's review the code. int arr [] = {1,2,3,4}; int x = arr[0]; // At this point <x> contains value 1 (element at index 0) cout << arr[x]; // Since <x> value is 1, arr[x] means arr[1], whose value is 2. Hth, cmiiw
27th May 2019, 8:59 PM
Ipang
+ 3
int arr []={1,2,3,4}; int x=arr[0]; //arr[0]==1 so x == 1 cout << arr[x]; // arr[1] == 2 #
27th May 2019, 9:21 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 3
Thank you all for taking time out of your day to answer my question.
27th May 2019, 9:33 PM
Josiah Mitchell
Josiah Mitchell - avatar
+ 1
I tried printing the index 0 of my array, but the index to the right prints instead. Why does this happen?
27th May 2019, 8:45 PM
Josiah Mitchell
Josiah Mitchell - avatar