I am not able to understand why both values are different and how does it change?
int a[100]
for(int i=0; i<100; i )
cin >> a[i];
int i=20;
cout << a[i 1] << endl;
cout << a[i] ;
I tried to know value of both
CodePudding user response:
a[i 1]
that means value of i 1
th index. which means, lets suppose if i=20
and it has value of 50 and a[21] has value of 200
then a[i 1] means a[i 1]=200
so when it's about a[i 1]
then it point to the next
and a[i]
that means increase 1 by ith
index value.
so i=20
and a[i]=50
and a[i]
means 50 1
which is 51
CodePudding user response:
When you declare this int a[100], it contains 100 zeroes.
In the for-loop, those 100 zeroes are output.
Even after assigning i manually to 20, a[20 1] still contains the value zero. The expression a[i] is actually increasing the value of the index 20 by saying a[20] = a[20] 1
CodePudding user response:
a[i 1] you just accessing the index of i 1 ;
example :
you have an array
[4,5,6,7] and i = 0 ;
cout<<arr[i 1]
means than you want to print arr[0 1] which have value 5 :
[4,5,6,7]
^
but when you do arr[i] that means that you modify the value of 4 to
become 5
in case if arr[i] simply you just add 1 to the value in arr[i]