+ 3

Why that result: 7

Here is task: int a[3]={3,2,2}; for (int i=1; i<3; i++){ a[0]+=a[i]; } cout<<a[0];

10th Jul 2020, 7:54 PM
Vladimir Kushner
Vladimir Kushner - avatar
3 Respostas
+ 21
a[0] = a[0]+ a[i] and initially a[0]= 3 so as the for loop starts initially i=1 so a[0]=a[0]+a[1] which is 3+2=5 now value of a[0]=5 furthermore value of i increase to 2 condition is still true now a[0]=a[0]+a[2] which is 5+2=7 and now i=3 and condition becomes false.. end of loop so output=7 Hope it helps...
10th Jul 2020, 8:03 PM
Shruti
Shruti - avatar
+ 3
Let's see: i = 1; a[0] = 3. i = 2; a[0] = 3+2=5. i = 3; a[0] = 5+2=7. So, the final a[0]=7.
10th Jul 2020, 8:04 PM
Alexander Koval🇧🇾
Alexander Koval🇧🇾 - avatar
+ 2
Firstly you declared a[0]=3, a[1]=2, a[2]=2, Then loop started. When i=1, then a[0]=a[0]+a[1]; a[0]=3+2=5; //a[1]=2 When i=2, a[0]=a[0]+a[2]; So a[0]=5+2=7;//a[2]=2 Then the loop will break; so when you print a[0] the answer is 7
11th Jul 2020, 8:01 AM
The future is now thanks to science
The future is now thanks to science - avatar