0

Why we are writing int sum=0; and sum+=arr[x]; there

#include <iostream> using namespace std; int main() { int arr[] = {11, 35, 62, 555, 989}; int sum = 0; for (int x = 0; x < 5; x++) { sum += arr[x]; } cout << sum << endl; return 0; }

8th Oct 2019, 3:52 PM
Ravi Henry
Ravi Henry - avatar
2 odpowiedzi
+ 3
This program is calculating sum of numbers present in array. int sum=0; Here, You are declaring variable sum and initializing it by 0 so that there is no garbage value. The variable sum will store the sum of numbers present in the array. sum+=arr[x]; It is same as writing: sum=sum+arr[x]; Here, You are calculating sum of numbers present in array. x is used to access the values stored in array arr[].
8th Oct 2019, 4:07 PM
Chetali Shah
Chetali Shah - avatar
+ 1
Got it thx
8th Oct 2019, 4:47 PM
Ravi Henry
Ravi Henry - avatar