0

how does sum is calculated in this

int arr[] = {11, 35, 62, 555, 989}; int sum = 0; for (int x = 0; x < 5; x++) { sum += arr[x]; } cout << sum << endl;

4th Oct 2018, 7:58 AM
Shadil saifi
Shadil saifi - avatar
6 Respuestas
+ 8
//1st iteration x = 0, 0 < 5 (true) sum += arr[x] --> 0 += 11 sum = 11 increments x --> x = 1 //2nd iteration x = 1, 1 < 5 (true) sum += arr[x] --> 11 += 35 sum = 46 increments x --> x = 2 //3rd iteration x = 2, 2 < 5 (true) sum += arr[x] --> 46 += 62 sum = 108 increments x --> x = 3 //4th iteration x = 3, 3 < 5 (true) sum += arr[x] --> 108 += 555 sum = 663 increments x --> x = 4 //5th iteration x = 4, 4 < 5 (true) sum += arr[x] --> 663 += 989 sum = 1652 increments x --> x = 5 //6th iteration x = 5, 5 < 5 (false) terminates loop sum = 1652
4th Oct 2018, 11:19 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 9
Explanation of something similar, hope it helps: https://www.sololearn.com/Discuss/400109/?ref=app
4th Oct 2018, 9:21 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
your welcome
4th Oct 2018, 11:29 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
thanks ahmad
4th Oct 2018, 11:28 AM
Shadil saifi
Shadil saifi - avatar
0
Thorught the "for" loop that add all "arr" items to sum (inited to 0 at start)
4th Oct 2018, 8:45 AM
KrOW
KrOW - avatar
0
although it helped in understanduling the concept but the link u provided is for java
4th Oct 2018, 10:27 AM
Shadil saifi
Shadil saifi - avatar