Professional Documents
Culture Documents
Chap5 HW
Chap5 HW
Chap5 HW
CSCI-313
HW 5
R-5.10
We can define a recursive method called arraySum. The method takes the array and
the size 'n' as parameters. In the base case, when 'n' is 0, the method returns 0
as there are no elements to sum. Otherwise, it initializes a variable sum to 0 and
iterates through each element in the array, adding it to the sum. It then makes a
recursive call to arraySum with the updated size 'n-1' and adds the returned sum to
the current sum. Finally, it returns the sum, which represents the sum of all
elements in the array
-----------------------------------------------------------------------------------
--------------------------------------
C-5.16 -
To solve the Towers of Hanoi puzzle recursively, we can define a function called
hanoi that takes four parameters: the number of disks 'n', the source peg 'a', the
destination peg 'c', and the auxiliary peg 'b'. The base case is when 'n' is 1,
where we move the disk directly from the source peg to the destination peg. For
larger 'n', we recursively move 'n-1' disks from the source peg to the auxiliary
peg using the destination peg as temporary storage, then move the remaining largest
disk from the source peg to the destination peg, and finally recursively move the
'n-1' disks from the auxiliary peg to the destination peg using the source peg as
temporary storage.
-----------------------------------------------------------------------------------
--------------------------------------
P-5.28
import java.util.*;
if (!solutionFound) {
System.out.println("No solution found!");
}
}
assignment.remove(letter);
availableDigits.add(digit);
}
-----------------------------------------------------------------------------------
--------------------------------------
P-5.29
return tickLine.toString();
}