int main(){ int arr[20], arraysize, sub, i; printf("Enter the size of array\n"); scanf("%d", &arraysize); printf("Enter the element of the array\n"); for(i = 0; i < arraysize; i++){ scanf("%d", &arr[i]); } printf("\n\n"); printsubset(arr, arraysize); return 0; }
/* a : pointer to an array of frequency_t elements. N : size of the above array. sum : sum we are looking for. result : array where selected numbers will be stored. index : caller must initially setup result and index. */ void subsetsum(frequency_t* a, int N, int sum, int* result, int index) { if(N == 0) return; int psum = sum - a[0].item; //we cannot use an element if its frequency count is <= zero. if(a[0].count <= 0) goto try_with_next_element; result[index] = a[0].item; //if psum is zero, we found a combination which adds up to sum, so print it. otherwise decrease the frequency //count of the current element and make a recursive call. if(psum == 0) printsubset(result, index + 1); else { --a[0].count; subsetsum(a, N, psum, result, index + 1); ++a[0].count; } try_with_next_element: subsetsum(a + 1, N - 1, sum, result, index); }