I have to do a program that reads a list of positive numbers (5,3,6...) and a target. The program should find the target value (if posible) through combinations of sums and differences of the given number list. Also it should store in an array the combination of numbers and its sign. This is what I have:
The problem is that the program doesnt find all the combinations, for example, given the following list (2,3,10,4,8) and target = 16, the variable sum never reaches the target, (the combination of numbers to reach the target is 2,10,4) but if the target = 15, the variable sum reaches the target.Code:void findobj(int j) { int i,temp; i = 0; do{ i++; if (numbers[i] != KEY) { //the variable "sum" stores the sum of the combination numbers sum+= numbers[i]; //stores solution numbers solution[j] = numbers[i]; temp = numbers[i]; //KEY is a macro wich value is -1, //it prevents the program from selecting //the same number twice numbers[i] = KEY; if (j < N) { findobj(j+1); numbers[i] = temp; if (sum != objetive) { sum-=numbers[i]; solution[j] = -(numbers[i]); } } } }while ((i < N) && (sum != objetive)); return; }
Thanks.