This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up**This preview shows
pages
1–2. Sign up
to
view the full content.*

// Arup Guha
// Edited 6/13/05 and 9/21/04 (not known when this was orignally
// written.) Added recursive code to solve the change problem.
// Re-editied on 6/14/05 to fully generalize the code to work for
// any denominations array (in increasing order).
/
public class change2 {
public static int[] denominations = {1, 2, 4, 7};
public static void main(String[] args) {
System.out.println("rec: "+makeChangerec(15, 7));
System.out.println("dyn: "+makeChangedyn(15, 7));
}
public static int makeChangedyn(int n, int d) {
// No way to make change for a negative amount.
if (n < 0)
return 0;
// We can make change for i cents using the smallest coin only
// if i is divisible by this coin amount.
if (n%denominations[0] == 0)
return 1;
else
return 0;
// Build table here.
else {
int[][] table = new int[denominations.length][n+1];
// We can make change for i cents using the smallest coin only
// if i is divisible by this coin amount.
for (int i=0; i<n+1;i++)
if (i%denominations[0] == 0)
table[0][i] = 1;
else
table[0][i] = 0;
// We can make change for 0 cents in 1 way.

This
** preview**
has intentionally

This is the end of the preview. Sign up
to
access the rest of the document.

Ask a homework question
- tutors are online