int combiset(int ism,int inarr[],int inpth){
int r1,r2,r3,r4;
for(int i=0;i<inpth;i++){
r1 = inarr[i]+inarr[i+1];
r2 = r1+inarr[i+2];
r3 = r2+inarr[i+3];
r4 = r3+inarr[i+4];
if(r4==ism){
printf("%d %d %d %d %d\n",inarr[i],inarr[i+1],inarr[i+2],inarr[i+3],inarr[i+4]);}
if(r3==ism){
printf("%d %d %d %d\n",inarr[i],inarr[i+1],inarr[i+2],inarr[i+3]);}
if(r2==ism){
printf("%d %d %d\n", inarr[i], inarr[i+1], inarr[i+2]);}
if(r1==ism){
printf("%d %d\n", inarr[i], inarr[i+1]);}
}
}
I am currently designing a function which will calculate and display out the possible combinations of sets from the user input. User will inputs the value of sum that they want to target and the array data such as length and integers inside. For example, if user inputs
array length:10
array data:9 1 5 5 8 9 7 3 1 6
target sum:10
then the result output should look like
9 1
5 5
7 3
3 1 6
Hereby, answer should follow the input array order and answers should be linked, so 1 5 3 1 cannot be an answer.
In my opinion, function which I have designed is too inefficient because user may input the array length as 100 then I have to make r1 until r100 to check whether there is possible combination of sets but with my method, the codes will get tremendously longer. Thus I am thinking of simpler code but I have no idea for it. How can I make my function better to find possible combinations of set?