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

JAD Advantages and Disadvantages

Examine the collapse of Han. What factors explain how this occurred? How influential in Han Dynasty on Chinese History?

Summarize effective communication strategies in organizational hierarchies, and review the role and relevance of the organizational grapevine