What am I missing?

coldmeat

Diamond Member
Jul 10, 2007
9,234
142
106
This is from a beginner programming course that I'm taking as an elective. This is a question from a practice quiz.

I must be missing something super obvious...

AIEbI.jpg


wtf?
 

CPA

Elite Member
Nov 19, 2001
30,322
4
0
my guess is the float, instead of using int

But, i haven't done programming in years.
 
Last edited:

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
Yeah both of those you have the right answer.

EDIT:

Meaning the X is right and the blue arrows are wrong.
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
I'm not a programmer, but I know that a semicolon is a statement terminator.

The second line does nothing, the third line sets X=0

0+2=2 last time I checked :)

There you go. Much better answer than LOL.
 

coldmeat

Diamond Member
Jul 10, 2007
9,234
142
106
I'm not a programmer, but I know that a semicolon is a statement terminator.

The second line does nothing, the third line sets X=0

0+2=2 last time I checked :)

damn, I didn't notice that, and I know that semicolons are one of the things I should be constantly checking.
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
damn, I didn't notice that, and I know that semicolons are one of the things I should be constantly checking.

It makes sense on that second one. The first one should still work, though. If X was declared as an integer it would never reach 1.1 but floats should be. Either float or double you should be able to reach 1.1. Integers truncate.

EDIT:

I would just put it in a program and see what happens. Based on the printf statement I'm assuming its just C.
 

KaChow

Senior member
Nov 21, 2006
219
0
71
That quiz question seem correct to me, why do you think 4 is the correct answer?

The ; after the first if ends the statement. The next statement resets x to 0. The next if-else is testing x against 3 but it is 0 so it adds 2...therefore, x = 0 + 2 or simply 2.

If the ; wasn't placed after the first if statement the condition to set x to 0 wouldn't be met and x would still == 3. The final if-else would add 1 and the answer would be 4.
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
Well I couldn't see it cause it was moving so fast, but it was printing numbers with 6 decimal places

Interesting to know. Mathematically 1.1 does equal 1.100000. Never would have thought a computer wouldn't be able to figure that out.

EDIT:

I added this to a compiler and it works if you have while(x < 1.1). It will quit when it hits 1.1. That is messed up.
 
Last edited: