What is the value assigned to the variable in each of these cases? Explain curious results. Be careful!

What is the value assigned to the variable in each of these cases? Explain curious results. Be careful!

int x, y;
a) x = 1/2;
b) y = 3.0/2.0;
double z, w, t;
c) z = 1/2;
d) w = 3/2;
e) t = 3.0/2.0;

Answer:
a) 0 is assigned.1/2 is computed using truncating integer divide which gives 0.
b) 1.5 is calculated and assigned. The fractional part is discarded; 1 is stored.
c) 0 is calculated because 1/2 is done with truncating integer divide, gives 0. The floating point value 0.0 is stored.
d) 1 is calculated, because truncating int divide, 1.0 is stored
e) 1.5 is calculated and stored.

Comments

Popular posts from this blog

What is JAD and how are users involved? What are advantages and disadvantages of JAD?

What are the three dimensions to business problems? Give an example of each.

A laboratory assistant prepared solution of 0.8 M, 0.6 M, 0.4 M, and 0.2 M sucrose, but forgot to label them. After realizing the error, the assistant randomly labeled the flasks containing these four unknown solutions as flask A, flask B, flask C, and flask D.