0

can someone explain this ? a[0]=3?

int main() { int a[3]={1,2,3,}; for(int i=0;i<3;i++){ a[0]+=a[i]; } cout<<a[0]; //Output 6 }

9th May 2019, 8:57 AM
San Anemos
San Anemos - avatar
7 Respostas
+ 3
I would recommend taking a look at the supposed output... (imho 4+3 is not 6, but what do I know)
9th May 2019, 9:22 AM
Thoq!
Thoq! - avatar
+ 2
Array a[3] is assigned 3 numbers: "1, 2, 3". => а=[1, 2, 3]; Further, using the cycle a[0] is assigned the sum of these numbers. => а=[7, 2, 3]; 1. а[0]=1+1; 2. а[0]=2+2; 3. а[0]=4+3; // 7
9th May 2019, 9:07 AM
Solo
Solo - avatar
+ 2
Antonio Mariani stuff like that happens to me often enough too
9th May 2019, 10:05 AM
Thoq!
Thoq! - avatar
+ 1
Grigoriadis Georgios, Antonio Mariani Explain to me how you managed to get the answer 6? 😂 👇 So 0: a[0]+a[0] //1+1;a[0]=2 1: a[0]+a[1] //2+2;a[0]=4 2: a[0]+a[2] //4+3;a[0]=6 ??? 😂 output: a[0]=6 ->output=6
9th May 2019, 9:23 AM
Solo
Solo - avatar
0
Thanks for answers.
9th May 2019, 10:47 AM
San Anemos
San Anemos - avatar