Can someone pls explain this step by step?? This question was in sololearn but I changed sum=1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone pls explain this step by step?? This question was in sololearn but I changed sum=1

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

29th Dec 2019, 11:20 AM
👨‍💻👑
8 Answers
+ 2
When sum=0 //output 1652 When sum=1 //output 1653 What did I say in my previous answer. Come on its basic maths.
29th Dec 2019, 11:34 AM
Avinesh
Avinesh - avatar
+ 1
Your answer would have increased by 1 when you made sum=1 when compared to sum=0.
29th Dec 2019, 11:28 AM
Avinesh
Avinesh - avatar
29th Dec 2019, 11:39 AM
Avinesh
Avinesh - avatar
0
When sum=0 I found the solution But when I tried to do it with 1 ,I got stuck I don't know if i got my theory wrong Plss help
29th Dec 2019, 11:22 AM
👨‍💻👑
0
What was the result you expected? And what happened instead?
29th Dec 2019, 11:24 AM
HonFu
HonFu - avatar
0
The output is 1653
29th Dec 2019, 11:31 AM
👨‍💻👑
0
Is this how you do it? Loop 1# x=0 0<5=true ( X=x+1 )x=0+1=1 Sum=sum+arr[0] =1+11 =12 Loop 2# x=1 1<5 =true X=1+1=2 Sum=1+arr[1] ... I don't know if that's how it is??
29th Dec 2019, 11:37 AM
👨‍💻👑
0
sum gets the salue you start from. Say it's 0. Then, with each run of the loop, arr[i] is added to sum. If you give sum a higher initial value, the end result will naturally also be higher. So if you set sum to 1 before the loop, the result will be 1 higher.
29th Dec 2019, 11:53 AM
HonFu
HonFu - avatar