This preview shows pages 1–2. Sign up to view the full content.

Answer to Final CS100J, Fall 2005 NAME David Gries NET ID djg page 1 Question 1. int h= –1; int p= –1; while (p != k) { p= p + 1; // if b[0. .h] does not contain a dup for b[p], // then move b[p] into the first segment if (h < 0 || b[h] != b[p]) { h= h+1; b[h]= b[p]; } } Question 2. This answer makes use of conditional expressions to give a simple solution. public static int [][] reduceArray ( int [][] b, int r, int c) { int res[][]= new int [b.length-1][b[0].length-1]; /** copy elements of b to res */ for ( int i= 0; i != res.length; i= i+1) { for ( int j= 0; j != res[i].length; j= j+1) { res[i][j]= b[i < r? i: i+1][j < c ?j : j+1]; } } return res; } Question 3a. mean( v( v > 0) ) 3b. % Store the powers of x in powerx powerx = (x*x) .* cumprod(x + zeros(1, n)); % store in s the signs of the terms s = -1 * cumprod(-1 + zeros(1,n)); % Store the denominators in denom fact = cumprod(1:(2*n – 1)); denom = fact(1:2:(2*n – 1)); % Store the solution in sol sol = cumsum(signs .* powerx ./ denom); Question 4. a. 22

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.