int one() { int n,m; n = 4; m = 6; while(n > 0) { n = n - 1; m = m + 2; } return m; }Answer: c. If you look at the values that n and m take on each time the function reaches the top of the loop, you get
n | m | |
4 | 6 | |
3 | 8 | |
2 | 10 | |
1 | 12 | |
0 | 14 |
int f(int x) { if(x < 2) { return x + 1; } else { return 2*x + 1; } }Answer:
f(1) = 1+1 = 2 f(f(1)) = f(2) = 2*2+1 = 5 f(f(f(1))) = f(5) = 2*5 + 1 = 11
void r(int a, int x, int &z) { cout << r: a = " << a << " x = " << x << " z = " << z << endl; a = 2; x = 5; z++; } void t() { int a, b, x, z; a = 9; b = 20; x = 67; z = 92; r(a,b,x); r(a,b,x); cout << "t: a = " << a << " b = " << b << " x = " << x << " z = " << z << endl; }Answer: The key to answering this question correctly is to be careful, and not to be sloppy. Be sure to keep the two functions separate. Their variables have the same names, but they are different variables. t starts by setting its variables as follows.
t ------ a = 9 b = 20 x = 67 z = 92Now t calls r(a,b,x). Notice that the first two parameters of r are call-by-value parameters, but the third is a call-by-reference parameter. After starting r, but before running r, the situation is as follows.
t r ------ ------- a = 9 a = 9 b = 20 x = 20 x = 67 z = (reference to t's variable x) z = 92The first thing that r does is print the values of a, x and z. So the first line printed is
r: a = 9 x = 20 z = 67Now, after r runs, just before it returns, the situation is as follows.
t r ------ ------- a = 9 a = 2 b = 20 x = 5 x = 68 z = (reference to t's variable x) z = 92Notice that, when r adds 1 to z, it actually adds one to t's variable x, since z is a reference to t's variable x. When r changes one of its call-by-value parameters, only its local memory is changed.
Now r returns, leaving t looking like this.
t ------ a = 9 b = 20 x = 68 z = 92Now t again calls r(a,b,x). The effect is similar to the first call; it prints
r: a = 9 x = 20 z = 68then adds one to t's variable x. So now t looks like this.
t ------ a = 9 b = 20 x = 69 z = 92Finally, t prints
t: a = 9 b = 20 x = 69 z = 92In summary, what is printed is
r: a = 9 x = 20 z = 67 r: a = 9 x = 20 z = 68 t: a = 9 b = 20 x = 69 z = 92
int mystery(int n) { if(n == 0) return 0; else return mystery(n-1) + n; }Answer: Notice that, in order to compute mystery(4), we will need to know the value of mystery(3). But in order to compute mystery(3), we will need to know mystery(2). This keeps going on until we need to know the value of mystery(0). The easiest way to find mystery(4) is to start with mystery(0) and work up.
mystery(0) = 0 mystery(1) = mystery(0) + 1 = 0 + 1 = 1 mystery(2) = mystery(1) + 2 = 1 + 2 = 3 mystery(3) = mystery(2) + 3 = 3 + 3 = 6 mystery(4) = mystery(3) + 4 = 6 + 4 = 10To answer this question correctly, just keep your work well organized. Do not scrawl a bunch of values around the page so that you lose track of what you are doing.
Note that you can get the rightmost digit of a number n by taking the remainder when you divide n by 10, and you can remove the rightmost digit by dividing n by 10.
For this problem, do not use any form of loop, and do not alter
the value of any variable that already has a meaningful value.
Answer:An obvious case to try is 0 (where there are no more digits to
look at.) Clearly, 0 contains no 7's. Another simple case is where
the rightmost digit of n is a 7: then n obviously contains a 7.
But what if n > 0, and the righmost digit is not a 7? Then just
ask a friend whether any of the other digits are a 7. For example,
to test whether 37829 contains a 7, you notice that the rightmost
digit (9) is not a 7. So you ask a friend whether 3782 contains a 7.
Your friend says yes. So you say yes, 37829 contains a 7.
(Don't worry about how your friend figures out that 3782 contains a 7.
That will take care of itself.)
int sevens(long n)
{
if(n == 0) return 0;
else if(n % 10 == 7) return 1;
else return sevens(n / 10);
}
Incidentally, what would happen if the test for n == 0 were left
out, and the function were written as follows?
int sevens(long n)
{
if(n % 10 == 7) return 1;
else return sevens(n / 10);
}
Remember, you MUST be sure to ask your friend to solve a smaller
problem than the one you are asked to solve. If n is 0, then this
solution asks your friend also to compute sevens(0). Your friend
then asks his or her friend for sevens(0), and this goes on forever.
The function never produces an answer.
You must not ask your friend to solve the
exact same problem as you are solving.