please explain and obtain output of this array. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

please explain and obtain output of this array.

int b[]={1,4,9,5,7}; int sum=0; for(int i=0;i<5;i++){ if(i%2==0){ sum+=b[i]; } cout<< sum;

29th Jun 2016, 2:25 PM
Devi Hota
5 Answers
+ 1
when i =0 sum=0+1 when i =1 but 1%2 is not equal to zero when i= 2 sum = 1+9 when i = 3 but 3%2 is 1 which is not zero when i = 4 sum = 7+10 then It comes out of loop and sum= 17 is printed
29th Jun 2016, 4:09 PM
BARUN SARRAF
BARUN SARRAF - avatar
0
write the size in the array b as 5 when the value of i is even then at each even value of array is added in sum when i=0 sum = 0+1; when i=2 sum =1+9; when i=4 sum =10+7; the output is 17 and add one more curly Bracket before cout
29th Jun 2016, 3:38 PM
Infinity
Infinity - avatar
0
there is an open bracket that has not been closed. the "for" one. to be able to compile it remove the opening parenthesis of "if" or put a closed one before the cout.
29th Jun 2016, 6:39 PM
Garme Kain
Garme Kain - avatar
0
You have to structurate your code to be more ease comprensible.
29th Jun 2016, 9:14 PM
RAZLESS34
RAZLESS34 - avatar
0
4%2 is also 0.so why not it get included?
30th Jun 2016, 2:31 PM
Devi Hota