finalspring2005answers - CS100J Spring 2005. Answers to...

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

View Full Document Right Arrow Icon
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=
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/01/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell University (Engineering School).

Page1 / 2

finalspring2005answers - CS100J Spring 2005. Answers to...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online