trying to understand recursion

May 14 2017 7:49 PM
the code below i am trying to use to understand recursion but the thing is when it trys to generate the possible combination of numbers that add up to 165 it gives me wrong combinations when numberOfsubsets = 4.
numberOfSubsets is simply the possible n combination of numbers(in this instance n is 4) that add up to 165. i chose 165 as it can be searched for from the given array of numbers.
the given array of numbers i supplied has to be used as for example i got output of
2 + 1 + 67 + 96 = 165 but it actually sums up to 166 and as such should not print yet somehow despite my condition it goes ahead to print which leaves me a bit confused.
and subsequent sums printed out are not correct yet it prints despite my condition not to print if it is not equal to 165.
i try to make the code faster by summing the numbers up the element excluding the last element and then as the last element is changing i simply add sum to it so this way i dont have to go through the entire array summing again everytime the last element is changed.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. class RecursiveSubsetSumEfficient    {  
  7.         /// <summary>  
  8.         /// program that finds all subsets of numbers in the array, which have a sum N. For example,   
  9.         /// if we have the array {2, 3, 1, -1} and N=4, we can obtain N=4 as a sum in the following two ways: 4 = 2+3-1; 4 = 3+1.  
  10.         /// </summary>  
  11.   
  12.   
  13.         static int numberOfElements = 0, numberOfSubsets = 1, sumOfNumberToFind = 0, count = 0, sum = 0;  
  14.         static int[] subsets, inputValues;  
  15.   
  16.   
  17.         static void GenerateSubsetWithSum(int currentLoop, int currentLoopValue, int boundary)  
  18.         {  
  19.             //here we generate combinations for 1,2..N number of elements  
  20.             while (numberOfSubsets <= numberOfElements)  
  21.             {  
  22.                 subsets = new int[numberOfSubsets];  
  23.                 GenerateSubsets(currentLoop, currentLoopValue, numberOfSubsets);  
  24.                 numberOfSubsets++;  
  25.             }  
  26.         }  
  27.   
  28.   
  29.         static void GenerateSubsets(int currentLoop, int currentLoopValue, int boundary)  
  30.         {  
  31.             if (currentLoop == numberOfSubsets)  
  32.             {  
  33.                 if ((sum + (inputValues[subsets[currentLoop - 1]])) == sumOfNumberToFind)  
  34.                 {  
  35.                     printSubsetsWithSum();  
  36.                 }  
  37.                 return;  
  38.             }  
  39.   
  40.   
  41.             //ensures that all elements prior to the last element in the array is calculated just once and used   
  42.             //through out until its no longer needed.  
  43.             if (currentLoop != 0 && currentLoop == (numberOfSubsets - 1))  
  44.             {    
  45.                 if(sum == 0)  
  46.                  {   
  47.                    sum = checkSumOfNumbers();  
  48.                  }  
  49.                  else  
  50.                  {   
  51.                      sum += inputValues[subset[currentLoop - 1]];  
  52.                  }  
  53.             }  
  54.             for (int i = currentLoopValue; i <= (numberOfElements - boundary); i++)  
  55.             {  
  56.                 subsets[currentLoop] = i;  
  57.                 GenerateSubsets(currentLoop + 1, i + 1, boundary - 1);  
  58.             }  
  59.             if (currentLoop != 0)  
  60.             {  
  61.                 sum = sum - inputValues[subsets[currentLoop - 1]];  
  62.              }  
  63.         }  
  64.   
  65.   
  66.         static void printSubsetsWithSum()  
  67.         {  
  68.                 count++;  
  69.                 Console.WriteLine("count so far {0}", count);  
  70.                 for (int i = 0; i < subsets.Length; i++)  
  71.                 {  
  72.                     if (i == 0)  
  73.                     {  
  74.                         Console.Write("{0}", inputValues[subsets[i]]);  
  75.                     }  
  76.                     else  
  77.                     {  
  78.                         Console.Write(" + {0}", inputValues[subsets[i]]);  
  79.                     }  
  80.                 }  
  81.                 Console.Write(" = {0}", sumOfNumberToFind);  
  82.                 Console.WriteLine();  
  83.         }  
  84.   
  85.   
  86.         static int checkSumOfNumbers()  
  87.         {  
  88.             int total = 0, value = 0;  
  89.             //check to see if we have a sum of the Number N.  
  90.             for (int j = 0; j < subsets.Length - 1; j++)  
  91.             {  
  92.                 value = subsets[j];  
  93.                 total += inputValues[value];  
  94.             }  
  95.             return total;  
  96.         }  
  97.         static void Main(string[] args)  
  98.         {  
  99.             inputValues = new int[] {2,3,1,10,17,39,20,41,67,109,46,56,92,110,38,97,42,32,42,63,51,23,54,96,88,18,23,82,51};  
  100.             sumOfNumberToFind = 165;  
  101.             numberOfElements = inputValues.Length;  
  102.             GenerateSubsetWithSum(0, 0, numberOfSubsets);  
  103.             Console.WriteLine("finshed on time");  
  104.             Console.ReadLine();  
  105.         }  
  106.     }