void f2() { int i = 100; while(i != 25) { ... i = i + 1; } cout << i; }
The only place to print something is after getting out of the loop, where i is printed. But the only way to get out of the loop is to find that i = 25.
int m,n; n = 0; m = 0; while(n < 10) { n++; m = m + n; }
NOTE: There was an error in this problem -- it did not give an initial value to m. So m had a junk value in it. I have corrected the error here, setting m to 0 initially.
Notice that the last time through the loop, the loop body is started with n = 9, since n = 10 will cause the loop to exit. The loop body adds 1 to n, making it 10, and then adds 10 to m. So the largest value added into m is 10.
int x,y,z; y = 4; z = y - 1; if(y > z || z < 0) x = z - 1; else x = z + 1;
void four() { int x,y,z,w,b; x = 45; y = 2 * x + x * 3; z = x / 2; w = x % 2; b = x < y; cout << "x = " << x << " y = " << y << " z = " << z << " w = " << w << " b = " << b << endl; }
Answer: x = 45 y = 225 z = 22 w = 1 b = 1
double geometricMean(double x, double y) { return sqrt(x*y); }
z = geometricMean(r, 2*s);
int f(int x) { int k = 1; while(k <= x) { k = k + k; } return k; }What are f(3) and f(f(3))?
f(3) = 4 f(f(3)) = 8
Do a careful hand simulation. Since f(3) = 4, the second part is to compute f(4). Don't bother to compute f(3) again; you know it is 4.