CH 03
CH 03
Algorithms in Java™
Sixth Edition
Michael T. Goodrich
Department of Computer Science
University of California, Irvine
Roberto Tamassia
Department of Computer Science
Brown University
Michael H. Goldwasser
Department of Mathematics and Computer Science
Saint Louis University
Reinforcement
R-3.1) Hint Use a calculator to aid in the arithmetic.
R-3.2) Hint You have to have your random number select a random index
in the array, so be sure to keep track of the number, n, of entries in the
array and do not index past index n − 1.
R-3.3) Hint The alphabets for most alphabet-based languages are included
in the Unicode character encoding standard.
R-3.3) Solution The alpha array and the various uses of value 26 would
have be changed to the new alphabet and its size. In addition, all the
places that use the literal A to refer to the first letter would now have to
be changed to the first letter in the new alphabet. In this case, it would be
better to define a final static int FIRSTLETTER, set it to the first letter,
and use it instead of A. This assumes the messages are still in upper-case,
of course.
R-3.4) Hint You may want to add a new instance variable to track if the
game has been completed.
R-3.5) Hint Make the modification in the code and test it.
R-3.5) Solution There are no negative consequences if removing those
lines (in fact there would be an every so slight increase in efficiency with-
out). While the internal state may seem inconsistent with tail referencing
a defunct node, that tail reference is never accessed for an empty list, so
the inconsistency has no effect.
R-3.6) Hint It is okay to have an algorithm running in linear time.
R-3.6) Solution
16 Chapter 3. Fundamental Data Structures
Creativity
C-3.17) Hint You don’t need to sort A.
C-3.17) Solution
public int missing(int[ ] A) {
boolean[ ] found = new boolean[A.length]; // false, by default
for (int val : A)
if (found[val])
return val;
else
found[val] = true;
return −1; // shouldn't happen if input as expected
}
C-3.18) Hint It might help to sort B.
C-3.18) Solution Sort the array B then scan it from front to end looking
for the repeated entries. Each pair of repeated integers will be consecutive
in the sorted listing.
C-3.19) Hint Add items at the “end” of the contiguous run of objects
in the array. For removing an object, consider first swapping it with the
object at index n − 1.
C-3.19) Solution
public void add(GameEntry e) {
if (numEntries < board.length) {
board[numEntries] = e;
numEntries++;
} // otherwise no room for new entry
}
Projects
P-3.36) Hint Matrix addition is defined so that if C = A + B, then C[i, j] =
A[i, j] + B[i, j]. Matrix multiplication is defined so that if C = AB, where
A is a c × d matrix and B is a d × e matrix, then C[i, j] = ∑dk=0 A[i, k]B[k, j].
That is, C is a c × e matrix.
21
P-3.37) Hint You should keep track of the number of game entries explic-
itly.
P-3.38) Hint You should keep track of the number of game entries explic-
itly.
P-3.39) Hint You will probably need separate encrypt and decrypt arrays
for the upper- and lower-case characters.
P-3.40) Hint The original CaesarCipher implementation was already ef-
fectively a substitution cipher, with a specifically chosen encoder pattern.
P-3.41) Hint If you get the constructor to use the correct encoder string,
everything else should work.
P-3.42) Hint A good way to generate a random encryption array is to start
with the alphabet array. Then for each letter in this array, randomly swap
it with some other letter in the array.