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

CS100J Spring 2005. Answers to final 1. int k= 1; int big1= Math.max(b[0], b[1]); int big2= Math.min(b[0], b[1]); // inv:big1 is largest in b[0. .k], // big2 is 2nd largest in b[0. .k], // 0 < k < b.length while (k != b.length - 1) { if (b[k+1] > big1) { big2= big1; big1= b[k+1]; } else if ( b[k+1] > big2) { big2= a[k+1]; } k= k + 1; } //post: big1 is largest in b[0. .] and big2 is 2nd largest in a[0. .] 2. /** spec as on final */ public static int [][] genPascalTriangle(int n) { int [][] b= new int [n][n]; // inv: rows b[0. .k-1] have been // calculated for ( int k=0; k != n; k= k+1) { // calculate row k of triangle b[k][0]= 1; b[k][k]= 1; // inv: b[k][0. .j-1] and // b[k][k] have been // calculated for ( int j= 1; j < k; j= j+1) { b[k][j]= b[k-1][j-1] + b[k-1][j]; } } return b; } 3 (a) length(find(A > 0)) (b) function result = approxpi(n); nums= - ones(1, n); nums= (-4) * cumprod(num); dens= [1:2:(2*n-1)]; result= sum(nums ./ dens); 4 (a). numFile= numFile + 1; avgSize=

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.