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;
6 odpowiedzi
+ 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
+ 9
Explanation of something similar, hope it helps: https://www.sololearn.com/Discuss/400109/?ref=app
+ 6
your welcome
+ 1
thanks ahmad
0
Thorught the "for" loop that add all "arr" items to sum (inited to 0 at start)
0
although it helped in understanduling the concept but the link u provided is for java