127B_Quiz5

advertisement
C Sc 127B Quiz 5 Section Leader__________ Your Name ______________________
30-March-2007
1. Write the return values from each call to the method named mystery
___mystery(0)
___mystery(1)
___mystery(2)
30pts
(5pts)
___mystery(3)
___mystery(4)
public int mystery(int n) {
if(n < 1)
return 0;
else if (n == 1)
return 1;
else
return 2 * mystery(n - 1);
}
2. Using recursion, complete method oddDownEvenUp that first prints all odd integers from the largest odd
number <= argument down to 1. The same method call must then print all the even integers in the range
of 2 through the largest even integers <= argument. The following method calls shown to the left must
generate the output shown to the right. (10pts)
Method call
Required Output
oddDownEvenUp(1);
oddDownEvenUp(2);
oddDownEvenUp(3);
oddDownEvenUp(4);
oddDownEvenUp(5);
oddDownEvenUp(9);
1
1
3
3
5
9
2
1
1
3
7
2
2 4
1 2 4
5 3 1 2 4 6 8
public void oddDownEvenUp(int n)
3. Implement method harmonic so the given assertions compile and pass. (5pts).
1.0 if n  1
harmonic (n) 
harmonic (n - 1)  1.0 / n
if n  1
@Test
public void testHarmonic() {
assertEquals(1.0, harmonic(1));
assertEquals((1.0+1.0/2.0), harmonic(2));
assertEquals((1.0+1.0/2.0) + (1.0/3.0), harmonic(3));
}
4. Complete method sumPositives to return the sum of all array elements greater than 0. Use
recursion; do not use a loop. The given assertions must compile and pass. (10pts)
@Test
public void testSumpositives() {
int[] x1 = { 0, 4, 5, 0 };
assertEquals(9, sumPositives(x1, 4));
int[] x2 = { 1, -1, 2, -2, 3, -3 };
assertEquals(6, sumPositives(x2, 6));
}
public int sumPositives(int[] x, int n) {
Answers
1.
_0__mystery(0)
_1__mystery(1)
__2_mystery(2)
_4__mystery(3)
_8__mystery(4)
2.
public double harmonic(int n) {
if (n == 1)
return 1.0;
else
return harmonic(n - 1) + 1.0 / n;
}
3.
4. Three possible answers
private int sum;
public int sumPositives(int[] x, int n){
sum = 0;
addUp(x, 0, n);
return sum;
}
private void
if (i < n)
if (x[i]
sum +=
addUp(x,
}
}
addUp(int[] x, int i, int n) {
{
> 0)
x[i];
i + 1, n);
public int sumPositives(int[] x, int index)
{
if (index >= 0)
{
int result = x[index];
if (result < 0)
result = 0;
return result + sumPositives(x, index - 1);
}
else
return 0;
}
public int sumPositives(int[] x, int n) {
return addUp(x, 0, n);
}
private int addUp(int[] x, int i, int n) {
if (i >= n)
return 0;
else {
if (x[i] <= 0)
return addUp(x, i + 1, n);
else
return x[i] + addUp(x, i + 1, n);
}
}
The quizzes have been divided into two piles to grade"
Weird ones (Rick will grade)
Close ones (Will, Zach, and Josh)
I think that most of the one your guys have will be 4/4 for the structure. But -1 if you see something small wrong.
If you can't trace quickly, type them in but be careful
And if you spot a small error, they changes from no
output to something reasonable, -1 but use 6pts bonus.
In this case, somone may get 2/6 when at first there
was no output or a stack overflow
4pts Structure
+1 Base case correct
+1 Prints 1 at some point
+1 Recursive case attempt
+1 trying to go up and down
6pts output
-3 failing to print any odds
-3 failing to print any evens
-2 print evens backwards
-2 print odds backwards
-5 printing only n
-1 printing all but one number
-1 printing one number twice
-4 for this
5: 5 3 1
6: 5 3 1 3 1 1 2 4 6
-4 for this
5: 5 3 1 1
6:
-5 for this
5: 4 2
6: 6 4 2
Rick:
We finished grading the pile of quizzes that we had. The scores seemed ok (a
couple of 30s, but mostly mid-to-low 20s). We did find a couple of odd cases
and I have outlined how we assigned points for those cases below. I also
kept notes on whose quiz we encountered each of these cases on, so if our
point assignments are incorrect let me know and I can just look up the
person?s quiz and change the score accordingly. Everything else was pretty
straightforward.
1:
Half the values are duplicated (-1)
ie: oddsDownEvensUp(5) produced 5 3 1 2 2 4 4
2: Prints odd values right then only prints 2, but correct number of times
(-3)
ie: oddsDownEvensUp(6) produced 5 3 1 2 2 2
3:
For odd n output is ok, for even n just prints n (-3)
oddsDownEvensUp(5) produced 5 3 1 2 4
oddsDownEvensUp(6) produced 6
4:
A variable inside the recursive method should be reset (-1)
5:
Using only if(_) when if else(_) structure should be used (-1)
6:
For even n prints odds down, then 0, then odds up
ie: oddsDownEvensUp(6) produced 5 3 1 0 1 3 5
For odd n output is ok, except ends with n+1
ie: oddsDownEvensUp(5) produced 5 3 1 2 4 6
We did not enter the graded quiz scores into the spreadsheet yet; we figured
it would probably be better to do them all at once. If you finish grading
the rest of the quizzes before class on Monday, is there a time that one of
us could stop by with the rest of the quizzes. If the quizzes are not all
graded by the Monday lecture, that?s fine. We told them that the recursion
problems usually take a longer time to grade so they might not even be
expecting them back so early.
Joshua Schickling.
Download