Array question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Array question

what is the output of this code? int nums[] = {1, 3, 2, 4, 6}; if (nums [2] > nums [0]) nums [0] = nums [2]; else nums [0] = nums [4]; cout << (nums [0] + nums [2] + nums [3]) guys can someone please explain to me how the h*ll can output be 8?.. if nums [0] = num [4] = 6 nums [2] = 2 and nums [3] = 6, the output I get is 14!? cheers!

12th Nov 2019, 1:49 PM
Papa Muerte
Papa Muerte - avatar
2 Answers
+ 4
nums[2] is 2 nums[0] is 1 if(2 > 1) returns true -> num[0] = nums[2] = 2 Your array looks now: 2, 3, 2, 4, 6 nums[0] + nums[2] + nums[3] = 2 + 2 + 4 = 8 output: 8
12th Nov 2019, 2:29 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
thanx a bunch Denise...I thought that else statement was false so I calculated if statement! SoloLearn Q&A is faster then my IDE ;)
12th Nov 2019, 3:01 PM
Papa Muerte
Papa Muerte - avatar