Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since this is a recursive function, you should be placing a test to make sure that <code>length != 0</code> right at the start of the function so that you don't cycle through an entire loop, accessing memory that shouldn't be accessed (because at that point <code>command</code> is pointing to the end of the memory segment, and you would have done operations like assigning the value of <code>sum</code> from the value of what <code>command</code> is pointing to). </p> <p>Secondly, I don't see anywhere why your function needs to have a return type of <code>int</code>. It seems that <code>void</code> will work perfectly well, as you're not returning anything from the function (i.e., if you're at the end you simply are printing "NONE" and returning without any return value, so there is no need for the return-type to be <code>int</code> ... it should be <code>void</code>).</p> <p>So, since you're returning nothing (i.e, <code>void</code>), the first line in your function before you do anything else should look something like:</p> <pre><code>if (length == 0) { printf("NONE\n"); return; } </code></pre> <p>That way you catch where you need to stop the recursive calls. Then the last line of your function can be (you can omit the "return" before the recursive function call since you're returning <code>void</code>):</p> <pre><code>Addition(command+1, target, length-1); </code></pre> <p>Finally, I believe you're printing all these repeat values because you're incrementing through your <code>command</code> array incorrectly (it seems overly complex). You should simply need a single for-loop or while-loop where you match the first variable in <code>command</code> against the rest of the values and check if their sums equal the target.</p> <p>I've <a href="http://ideone.com/ejhWe" rel="nofollow">made a working copy of your function over at ideone</a>, so you can check out what I'm talking about, especially the simplification of the loop.</p> <p>Hope this helps,</p> <p>Jason</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload