title: C function to get all sums of an array using addition and subtraction
body: <p>What would be the best way (in C) to get all sums of N numbers in an array, by using addition and subtraction?</p>
  <p>For example (N = 3):</p>
  <pre><code>arr[] = [30, 14, 2]
  
  results:
  -30-14-2 = -46
  -30-14+2 = -42
  -30+14-2 = -18
  -30+14+2 = -14
   30-14-2 = 14
   30-14+2 = 18
   30+14-2 = 42
   30+14+2 = 46
  </code></pre>
  <p>As can be seen, there are 2^N solutions.</p>
  <p>I also noticed that the addition an subtraction symbols alternate in the same way as binary counting (000 001 … 110 111), which might be useful.</p>
  <p>Probably a recursive approach would be best, but I find it very hard to think recursively.</p>
  <p>Therefore, I hope someone can explain to me what the best strategy would be to tackle this problem.</p>
  <p>——————————</p>
  <p>EDIT:</p>
  <p>I have a working Python code, but this uses sets <code>set()</code>, which aren’t available in C. (<code>arr</code> is an array containing all numbers.)</p>
  <pre class="lang-py prettyprint-override"><code>out = set()
  out.add(0)
  for i in range(0, len(arr)):
      tmp = set()
      for j in out:
          tmp.add(j + arr[i])
          tmp.add(j - arr[i])
      out = tmp
  print(out)
  </code></pre>
  <p>——————————</p>
  <p>EDIT:</p>
  <p>With replacing the sets by arrays and making a few small changes, I got it working. Thanks to everyone who commented!</p>