finalfall2003answer

finalfall2003answer - CS100J Fall 2003 Answers to Final...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS100J Fall 2003 Answers to Final Have a happy holyday! 1 . k= m+1; j= n; /* invariant: b[m] = x, b[m+1. .j–1] <= x, b[k+1. .n] >= x */ while (j <= k) { if (b[j] <= x) { j= j+1; } else { Swap b[j] and b[k]; k= k – 1; } } // b[m] = x, b[m+1. .k] <= x, b[k+1. .n] >= x Swap b[m] and b[k]; 2. Note: such algorithms are actually used, when really large integers are to be maintained. The “base” would not be 10 but some other number like the larg- est int-1. If you are interested, look at Java API classes BigInteger and BigDecimal. ds= 0; d[ds]= 0; // invariant: as in question on final while (ds != Math.max(bs,cs)) { if (ds < bs) { d[ds]= d[ds] + b[ds]; } if (ds < cs) { d[ds]= d[ds] + c[ds]; } d[ds+1]= d[ds] / 10; d[ds]= d[ds] % 10; ds= ds+1; } if (d[ds] > 0) ds= ds+1; 3. /** = “array b is triangular”, i.e. each row i has 2*i+1 elements */ public static boolean isTriangular( int [][] b) { // invariant: rows 0. .k-1 are triangular for ( int k= 0; k != b.length; k= k+1) { if (b[k].length != 2*k + 1)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Ask a homework question - tutors are online