finalfall2006answers

finalfall2006answers - Answer to Final CS100J Fall 2005...

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

View Full Document Right Arrow Icon
Answer to Final CS100J, Fall 2005 NAME David Gries NET ID djg page 1 Question 1. /** Let x be the value currently in b[h]. Permute b[h. .k] and return an int j that satisfies b[h. .j-1] <= b[j] = x < b[j+1. .k]. */ public static int partition( int [] b, int h, int k) { int j= h; int t= k; /* inv: b[h. .j-1] <= b[j] = x <= b[t+1. .k] */ while (j < t) { if (b[j+1] <= b[j]) { Swap b[j+1] and b[j]; j= j+1; } else { Swap b[j+1] and b[t]; t= t-1; } } return j; } Question 2. int p= h; int q= p; while (q <= k) if (b[q] % 2 != 0) { b[p]= b[q]; p= p + 1; } q= q + 1; } Question 3a. h __a1__ 3b. /** see exam for spec */ public static void swap( int [][] b, int n, int h, int k, int p, int q) { for ( int r= 0; r != n; r= r+ 1) { for ( int c= 0; c != n; c= c+1) { // Swap b[h+r][k+c] with b[p+r][q+c] int temp= b[h+r][k+c]; b[h+r][k+c]= b[p+r][q+c]; b[p+r][q+c]= temp; } } } Question 4. /** = a string containing n occurrences of char c. Precondition: n
Background image of page 1

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

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

{[ snackBarMessage ]}

Page1 / 3

finalfall2006answers - Answer to Final CS100J Fall 2005...

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