Title: Midterm 2 Review
1Midterm 2 Review
2004
Notes on the CS 5 midterm
- Take-home exam due by 500 pm Sunday evening
(11/14)
- Hand in your solutions under the door of my
office, Olin 1265
- No computer use or other references are
permitted, except that you may use 1 (2-sided)
page of your own notes, which you should hand in
with your exam.
- 5 problems 2 what does this do and 3 writing
methods
- You may use up to 2 hours (in 1 block) for the
exam...
- These practice problems (and solutions) are at
www.cs.hmc.edu/dodds/cs5/mid2.ppt
2Practice Problem 1
Write a method that takes a String as input. The
method should return the number of vowels in the
input string. Count the letter y as a vowel
when there is no vowel before or after it.
Otherwise, count y as a consonant.
output returned
input yellowy
input slyly
output returned
output returned
input yesterday
3Practice Problem 2
What values do the variables c and A have at the
end of this block of code?
int c 0 int A new int4 A0 2 A1
0 A2 3 A3 1 for (int i3 igt0
--i) for (int j0 jlti j) if
(Aj gt Ai) int tmp Ai
Ai Aj Aj tmp c
4Practice Problem 3
Write a method with the following
signature First, the method should make sure
its input array has one more row than columns.
(If not, it should simply return 0). If so, it
should replace the last element in each column
with the whole columns sum. It should return the
largest value in the original array.
int spreadsheet(int A)
input
result
10 5 17 -2 11 6 1 10 42 0 0 0
10 5 17 -2 11 6 1 10 42 9 26 65
returns
42
5Pr. Problem 4
public static void main(String s) int A
new int2 A0 7 A1 4 H.pl(A0
A1) mix( A ) H.pl(A0
A1) A0 match(A1, A0) H.pl(A0
A1) mix( A ) H.pl(A0
A1)
What does this code print?
public static void mix(int A) A1
A0 A0--
public static int match(int x, int y) if ( x
lt y ) return (xy) else return (x-y)
6Practice Problem 5
Write a java method harmonic that takes a double
as input. It should returns the (integer) number
of initial terms in the harmonic sequence that
need to be added up so that the sum is greater
than the input double.
1 1 1 1 1
1 2 3 4 5
The harmonic sequence
7Solutions are on the following slides
8int numVowels(String s) int sum 0 int L
s.length() for (int i0 iltL i)
if (isV(s.charAt(i))) sum if
(s.charAt(i)y) if (i0 Lgt1
isV(s.charAt(1)) sum-- // front
else if (iL-1 Lgt1 isV(s.charAt(i-1))
sum-- // back else if (i!0 i!L-1
// middle
( isV(s.charAt(i-1)) isV(s.charAt(i1) ))
sum-- return sum boolean
isV(char c) if (cacecico
cucy) return true else return
false
Practice Problem 1
9c 0
intially
Practice Problem 2
A0
A1
A2
A3
c 1
i3, j0 (at end)
c 1
i3, j1 (at end)
int c 0 int A new int4 A0 2 A1
0 A2 3 A3 1 for (int i3 igt0
--i) for (int j0 jlti j) if
(Aj gt Ai) int tmp Ai
Ai Aj Aj tmp c
i3, j2 (at end)
c 2
c 2
i2, j0 (at end)
c 2
i2, j1 (at end)
i1, j0 (at end)
c 3
it sorts the array and counts the number of swaps
needed to do so!
10Practice Problem 3
int spreadsheet(int A) if (A.length !
A0.length1) return 0 int max A00
// make the first element the max int BOT
A.length-1 // the BOTtom row for (int
c0 cltA0.length c) // for each column, c
ABOTc 0 //
set the BOTtom to 0 for (int r0 rltBOT
r) // and row, r
ABOTc Arc // add the elements
above if (Arc gt max)
max Arc return
max
11A0
A1
Practice Problem 4
public static void main(String s) int A
new int2 A0 7 A1 4 H.pl(A0
A1) mix( A ) H.pl(A0
A1) A0 match(A1, A0) H.pl(A0
A1) mix( A ) H.pl(A0
A1)
6
11
public static void mix(int A) A1
A0 A0--
5
11
public static int match(int x, int y) if ( x
lt y ) return (xy) else return (x-y)
4
16
12Practice Problem 5
int harmonic(double d) int N 1
double sum 0.0 while (sum lt d)
sum 1.0/N N return
N