120 likes | 286 Views
Midterm 2 Review. 2004. Notes on the CS 5 midterm. Take-home exam due by 5:00 pm Sunday evening (11/14). Hand in your solutions under the door of my office, Olin 1265.
E N D
Midterm 2 Review 2004 Notes on the CS 5 midterm • Take-home exam due by 5:00 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
Practice 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
Practice Problem 2 What values do the variables cand A have at the end of this block of code? int c = 0; int[] A = new int[4]; A[0] = 2; A[1] = 0; A[2] = 3; A[3] = 1; for (int i=3 ; i>0 ; --i) { for (int j=0 ; j<i ; ++j) { if (A[j] > A[i]) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; c++; } } }
Practice 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 column’s 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
Pr. Problem 4 public static void main(String[] s) { int[] A = new int[2]; A[0] = 7; A[1] = 4; H.pl(A[0] + “ ” + A[1]); mix( A ); H.pl(A[0] + “ ” + A[1]); A[0] = match(A[1], A[0]); H.pl(A[0] + “ ” + A[1]); mix( A ); H.pl(A[0] + “ ” + A[1]); } What does this code print? public static void mix(int[] A) { A[1] += A[0]; A[0]--; } public static int match(int x, int y) { if ( x < y ) return (x+y); else return (x-y); }
Practice 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
int numVowels(String s) { int sum = 0; int L = s.length(); for (int i=0 ; i<L ; ++i) { if (isV(s.charAt(i))) sum++; if (s.charAt(i)==‘y’) { if (i==0 && L>1 && isV(s.charAt(1)) sum--; // front else if (i==L-1 && L>1 && isV(s.charAt(i-1)) sum--; // back else if (i!=0 && i!=L-1 && // middle ( isV(s.charAt(i-1)) || isV(s.charAt(i+1) )) sum--; } } return sum; } boolean isV(char c) { if (c==‘a’||c==‘e’||c==‘i’||c==‘o’||c==‘u’||c==‘y’) return true; else return false; } Practice Problem 1
1 1 1 1 2 0 1 3 2 2 3 2 2 3 0 0 0 0 0 0 1 1 2 3 2 3 3 3 c = 0 intially Practice Problem 2 A[0] A[1] A[2] A[3] c = 1 i=3, j=0 (at end) c = 1 i=3, j=1 (at end) int c = 0; int[] A = new int[4]; A[0] = 2; A[1] = 0; A[2] = 3; A[3] = 1; for (int i=3 ; i>0 ; --i) { for (int j=0 ; j<i ; ++j) { if (A[j] > A[i]) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; c++; } } } i=3, j=2 (at end) c = 2 c = 2 i=2, j=0 (at end) c = 2 i=2, j=1 (at end) i=1, j=0 (at end) c = 3 it sorts the array and counts the number of swaps needed to do so!
Practice Problem 3 int spreadsheet(int[][] A) { if (A.length != A[0].length+1) return 0; int max = A[0][0]; // make the first element the max int BOT = A.length-1; // the BOTtom row for (int c=0 ; c<A[0].length ; ++c) // for each column, c { A[BOT][c] = 0; // set the BOTtom to 0 for (int r=0 ; r<BOT ; ++r) // and row, r { A[BOT][c] += A[r][c]; // add the elements above if (A[r][c] > max) max = A[r][c]; } } return max; }
7 4 A[0] A[1] Practice Problem 4 public static void main(String[] s) { int[] A = new int[2]; A[0] = 7; A[1] = 4; H.pl(A[0] + “ ” + A[1]); mix( A ); H.pl(A[0] + “ ” + A[1]); A[0] = match(A[1], A[0]); H.pl(A[0] + “ ” + A[1]); mix( A ); H.pl(A[0] + “ ” + A[1]); } 6 11 public static void mix(int[] A) { A[1] += A[0]; A[0]--; } 5 11 public static int match(int x, int y) { if ( x < y ) return (x+y); else return (x-y); } 4 16
Practice Problem 5 int harmonic(double d) { int N = 1; double sum = 0.0; while (sum < d) { sum += 1.0/N; ++N; } return N; }