Ask your own question, for FREE!
Computer Science 17 Online
SourMunchkin7806:

Consider the following code segment. What is the value of x after execution of the code? double [] numList = {4.17, 3.11, 2.46, 1.57}; int x = 0; for(double n : numList) { x += (int)n * 2; }

SourMunchkin7806:

@Ultrilliam

SourMunchkin7806:

I put 23

Ultrilliam:

for future reference: ```c double [] numList = {4.17, 3.11, 2.46, 1.57}; int x = 0; for(double n : numList) { x += (int)n * 2; } ``` \```language code here \``` But anyway, taking this step by step: Go through each number, multiply it by 2, and add it all together, then you have x

Ultrilliam:

and remove anything past the decimal from each*

Ultrilliam:

so 4.17*2=8, rather than 8.34

SourMunchkin7806:

ok sooo its 20 then

Ultrilliam:

I get 21 actually

SourMunchkin7806:

But its 8+6+4+2

SourMunchkin7806:

8 and 2 is 10 then you jsut add the 6 for 16 and then the 4 to get 20

Ultrilliam:

The last one is 3,

1 attachment
SourMunchkin7806:

But i thought we removed anything past the decimal

SourMunchkin7806:

And 21 isnt an option

Ultrilliam:

Ah, I was doing it after it multiplied, my bad, lol

SourMunchkin7806:

So it is 20?

Ultrilliam:

1548178611-5c4754b3717a1cee3213a8-image.png

SourMunchkin7806:

Ok sweet I got a couple more can you help me with those i feel like im kidna getting it but they are all a bit different

Ultrilliam:

Sure

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
RAVEN69: My drawing so far is actually fire
1 week ago 9 Replies 2 Medals
PureSoulless: is staying at your friend's house while you're homeless legal.
2 weeks ago 5 Replies 1 Medal
whyjustwhy: i did that one TV girl trend with blake (aka @ShadowKid3)
1 week ago 12 Replies 2 Medals
whyjustwhy: i did that one TV girl trend with blake (aka @shadowkid3)
2 weeks ago 3 Replies 0 Medals
whyjustwhy: yo guys he can watch me sleep now (ignore dora)
3 weeks ago 24 Replies 1 Medal
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!