How its sum becomes 6? Can someone explain? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

How its sum becomes 6? Can someone explain?

int array[2][2]={0,1,2,3}; int i=0,sum=0; for(i=0;i<4;i++) { int x,y; x=i%2; if (x){ y=0; } else{ y=1; } sum+=array[x][y]; } printf("%d", sum);

28th Oct 2021, 9:35 AM
Bikesh Shrestha
3 Réponses
+ 3
Bikesh, Add this line before adding array[x][y] to <sum>. It might just give you an dea. printf("adding array[%d][%d] (%d) to <sum> -> %d\n", x, y, array[x][y], sum + array[x][y]);
28th Oct 2021, 10:33 AM
Ipang
+ 2
I am getting 0 how you are getting 6? https://code.sololearn.com/cB3Qr187t1lK/?ref=app
28th Oct 2021, 9:49 AM
A͢J
A͢J - avatar
+ 2
Ohh sorry! I just forget to put the values in curly brackets. Now it's corrected. Try once again.
28th Oct 2021, 10:12 AM
Bikesh Shrestha