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; }
8 Respuestas
+ 2
When sum=0 //output 1652
When sum=1 //output 1653
What did I say in my previous answer. Come on its basic maths.
+ 1
Your answer would have increased by 1 when you made sum=1 when compared to sum=0.
+ 1
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
0
What was the result you expected? And what happened instead?
0
The output is 1653
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??
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.