• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

What am I missing?

coldmeat

Diamond Member
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?
 
my guess is the float, instead of using int

But, i haven't done programming in years.
 
Last edited:
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.
 
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.
 
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.
 
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:
Back
Top