0

Arrays in loop and calculation

How should one interpret the line a[0] /= a[x]; in the following code? int main() { int a[3] = {9,4,2}; for(int x=1; x<2; x++) { a[0] /= a[x]; } cout << a[0]; From my understanding, a[0] is 9, the first element of the array. Is it correct that a[0] /= a[x] is the same as a[0] = a[0]/a[x]? What does the x in the square brackets of a[x] signify? --> I think this changes from 1 to 2 after the loop. The final output is 2... Totally confused.

19th Jul 2020, 7:43 AM
Solus
Solus - avatar
5 odpowiedzi
+ 2
the loop is run only once. so a[0] /= a[i] is a[0] = 9 / 4 == 2
19th Jul 2020, 7:49 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Solus If <i> in `a[0] /= a[i];` was mistaken for <x> (read: a typo), then I agree with Abhay and Bahha. Otherwise, I don't see the declaration/definition of variable <i> : )
19th Jul 2020, 8:39 AM
Ipang
+ 1
Ipang right, I didn't pay attention to " i ", brain autocomplete :) . by a[i] I meant a[x].
19th Jul 2020, 8:49 AM
Bahhaⵣ
Bahhaⵣ - avatar
0
a[0]=a[0]/a[9] a[0]=9/4 a[0]=2
19th Jul 2020, 7:49 AM
Abhay
Abhay - avatar
0
Bahha🐧 Yup, I also was wondering where <i> came from 😁
19th Jul 2020, 8:51 AM
Ipang