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; while(n < 10) { n++; m = m + n; }
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.
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);
void r(int x, int y, int& z) { cout << "r: x = " << x << " y = " << y << " z = " << z << endl; x = x + 1; z = y + x; } void t() { int a, b, x; a = 20; b = 25; x = 250; r(a,b,x); r(x,a,b); cout << "t: a = " << a << " b = " << b << " x = " << x << endl; }
Answer:
The key to getting this right is to do the hand simulation
carefully. Be sure to remember how to handle call-by-reference
and call-by-value. Before the first call to r, the picture looks
like this.
Now r(a,b,x) is called. The picture looks like this.
r: x = 20 y = 25 z = 250
r: x = 46 y = 20 z = 25
t: a = 20 b = 67 x = 46
t
----------
a = 20
b = 25
x = 250
t r
---------- ---------
a = 20 x = 20
b = 25 y = 25
x = 250 <- z .
|___________|
When function r runs, it prints line
r: x = 20 y = 25 z = 250
and then performs the two assignments. The picture looks like this.
t r
---------- ---------
a = 20 x = 21
b = 25 y = 25
x = 46 <- z .
|___________|
Now r returns, and t is about to call r again. The picture is like this.
t
----------
a = 20
b = 25
x = 46
Now t calls r(x,a,b). The picture looks like this just before r
runs.
t r
---------- ---------
a = 20 x = 46
b = 25 <- y = 20
x = 46 | z .
|___________|
Now r runs. It prints line
r: x = 46 y = 20 z = 25
Then r does the two assignments. The picture is like this.
t r
---------- ---------
a = 20 x = 47
b = 67 <- y = 20
x = 46 | z .
|___________|
Now r returns, leaving t ready to print its line. It prints
t: a = 20 b = 67 x = 46
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.
int mystery(int n) { if(1 == n) return 1; else return 3*mystery(n-1); }
You can find out the value of mystery(4) by computing mystery(1), mystery(2) and mystery(3) first.
mystery(1) = 1 mystery(2) = 3*mystery(1) = 3*1 = 3 mystery(3) = 3*mystery(2) = 3*3 = 9 mystery(4) = 3*mystery(3) = 3*9 = 27The key to getting this kind of question right is to stay organized, and not to be sloppy.
Answer: 27
int digitSum(int n) { if(n < 10) return n; else return digitSum(n/10) + n % 10 }
If n is a single digit number (n < 10) then the sum of the digits in n is just n itself. For example, digitSum(7) = 7.
Suppose that n has more than one digit. For example, n = 4567. Break this number into two parts: n%10 = 7 is the rightmost digit, and n/10 = 456 is the result of removing the rightmost digit from n. Then notice that
digitSum(4567) = digitSum(456) + 7 = 15 + 7 = 22That is, ask a friend to compute the sum of the digits in 456, and then add 7 to the result that your friend gives you. This is how the function definition works. The general rule for multi-digit numbers is
digitSum(n) = digitSum(n/10) + (n%10).