Sum of subsets backtracking
I'm trying to implement a version of subset sum using backtracking. From an array of coins, I want to obtain a certain sum. this is my code so far but I'm getting no output:
Code:
#include <stdio.h>
#define MAX 1024
int coins_array[] = {1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,10,10,10,10,10,15,15,15,15,15,15};
int N = sizeof(coins_array) / sizeof(coins_array[0]);
int S = 27, x[MAX];
void back(int step)
{
int Sol[100], total = 0;
for (int v = 0; v < N; v++)
{
Sol[step] = coins_array[v];
total += Sol[step];
for(int i = 0; i < step; i++)
{
if(Sol[i]+v <= S)
{
if(total == S)
{
printf("%d:\n", S);
printf("%d ", Sol[i]);
}
else
back(step+1);
}
}
total -= coins_array[v];
}
}
int main()
{
back(0);
return 0;
}
Any ideas?