hello, my code scans through an array , build sub-arrays, and print them out. the sub arrays contains numbers from original array,
that sums up to half the array sum. (ex: arr:1,2,3,4 subs:1,4 and 2,3)
i need to modify it so it would only print out the first sub-array that it finds, and the rest of the array (ex: 1,2,2,3,1,1 out will be: 1,2,2 and 3,1,1 only)
hope you can help me.
p.s. no static var are allowed
Code:#include <stdio.h> // printing all the options #define true 1 #define false 0 void printArr(int arr[], int n) { int i; puts(""); for (i=0; i<n; i++) printf (" %d",arr[i]); puts(""); } int SubsetSum(int *arr, int n, int S, int subseq[],int count) { int flag; if (S==0) { printArr(subseq,count); return true; } if (S<0 || !n) return false; subseq[count] = arr[0]; flag = SubsetSum(arr+1,n-1,S-arr[0],subseq, count+1); return (SubsetSum(arr+1,n-1,S,subseq,count) || flag); } void GetArray(int arr[], int i, int size) { int num=0; if (i < size) { printf("enter the %d element\n",i); scanf("%d",&num); arr[i]=num; GetArray(arr, i+1, size); } } void main() { int arr[20]={0}, subseq [12]; int S=0 ,arrsize=0,i; scanf("%d",&arrsize); GetArray(arr, 0, arrsize); for (i=0; i<arrsize; i++) S+=arr[i]; S=S/2; if (!SubsetSum(arr,arrsize,S,subseq,0)) printf("\nThere is no subsequence\n"); }



LinkBack URL
About LinkBacks


