+ 1
I do not understand the following code...?????
int arr[] = {11, 35, 62, 555, 989}; int sum = 0; for (int x = 0; x < 5; x++) { sum += arr[x]; } cout << sum << endl; //Outputs 1652
2 Respuestas
+ 8
//First iteration
//x = 0, 0 < 5 (true)
sum += arr[x] --> 0 += 11
//sum = 11
//Second iteration
//x = 1, 1 < 5 (true)
sum += arr[x] --> 11 += 35
//sum = 46
//Third iteration
//x = 2, 2 < 5 (true)
sum += arr[x] --> 46 += 62
//sum = 108
//Fourth iteration
//x = 3, 3 < 5 (true)
sum += arr[x] --> 108 += 555
//sum = 663
//Fifth iteration
//x = 4, 4 < 5 (true)
sum += arr[x] --> 663 += 989
//sum = 1652
//Sixth iteration
//x = 5, 5 < 5 (false)
exits from loop
Output:
1652
+ 13
This code is for sum the elements of an array so the output is 1652
sum += arr[x] where x is 0 to 4 so it will add elements of array as their index increase which is give 11+35+62+555+989=1652