Since my TA just quit after giving us this Lab i thought it wouldn't do much harm to ask for a little guidance on here.
The problem is I need to write a 2 dimensional dynamic programming solution to this subset sum problem. The number of ints in the set will be taken in first, followed by the target value. After taking in all the items in the set it will find 3 possible ways in the set to get the target value.
Such as if the target is 10, and the set is 5,5,2,4,6,8... the output would be 5,5...2,8...4,6
If there isnt 3 subsets that sum to the target then it will exit the program, if there is more than 3 it will just print 3 of them.
So far i have written this 1 dimensional subset sum solution.
Could anyone tell me how i would go about making it a 2 dimensional dynamic program, or explain it a little better in detail to me? From the little bit of info i got out of the TA before he quit, i think i need to make array C a 2D array and work from there. Is this a correct approach?
Any guidance would be much appreciated
// Get input sequence
int n; // Size of input set
int m; // Target value
int *S; // Input set
int *C; // Cost table
if (!S || !C)
printf("malloc failed %d\n",__LINE__);
S=0; // Sentinel zero
// Initialize table for DP
C=0; // DP base case
// For each potential sum, determine the smallest index such
// that its input value is in a subset to achieve that sum.
for (potentialSum=1; potentialSum<=m; potentialSum ++)
leftover=potentialSum-S[j]; // To be achieved with other values
if (leftover<0) // Too much thrown away
if (C[leftover]==(-1)) // No way to achieve leftover
if (C[leftover]<j) // Indices are included in
break; // ascending order.
C[potentialSum] = j;
C[potentialSum] = (-1);
// Output the input set
printf(" i S\n");
// Output the DP table
printf(" i C\n");
printf(" i S\n");