Nooby Question

My text wants me to predict what the value of x is after the statements are executed. The answer in the back gives 2.8; however, when I test it on my computer I don't get the same results.

double a=3.8,x;
int n=2, y;
x = (y = a/n)*2;

Heres my train of thought when doing it by hand.
First take the brackets.
So n is converted from an into to a double, but y is an int so y= 2?
Then multiply 2 and 2 to 4?
This train of thought is wrong, but I thought it would help in helping me find out what I'm doing wrong.
Thanks =D

Last edited on
y = a/n, y = 3.8/2, y = 1.9, y is an int so y = 1
Integer assignment truncates; it doesn't round.
How does the rounding work Zhuge. Does it always round down? And does that mean that my texbook is wrong to say that x = 2.8? Thanks
Like I said, it truncates. So yes, it always rounds down. Yes, I think your book is wrong; I worked it out in my head as I did above and tested it on MSVC++2008, both give x == 2 after the operations.
Sweet. Thanks. Thats what I came up with when I plugged it into Dev-C++.
Topic archived. No new replies allowed.