Note that there are some explanatory texts on larger screens.

plurals
  1. POtrying to write a recursive function that counts the number of sequences that sum up to that number C++
    primarykey
    data
    text
    <p>Okay, so here is what I'm trying to do. The user inputs a number. I'm trying to write a recursive function that counts the number of sequences that sum up to that number (user input).</p> <p>For example:</p> <p>Then the number of sequences that sum up to 6 is 11 (including 6 itself). </p> <pre><code>6 5+1 4+1+1 3+1+1+1 2+1+1+1+1 1+1+1+1+1+1 2+2+1+1 3+2+1 4+2 2+2+2 3+3 </code></pre> <p>I'm also trying not to have sequences that repeat, for example 2+2+1+1 and 1+1+2+2.</p> <p>The reason i have no code included is i cannot figure out a recursive way to make this work so i'm seeking some guidance. Thanks in advance!</p> <p>ADDITION:</p> <p>Okay so here is what my thought process is. 6 can be split as...</p> <pre><code>6 5+1 4+2 3+3 </code></pre> <p>but it is still not over,if you take 5+1 and consider the +1 part to be completed; you use the same trick to proceed.</p> <pre><code>4+1+1 3+2+1 </code></pre> <p>but then they start to repeat..... and i don't get further than this second step in my plan. </p> <p>Okay so code wise this is what I've come up with on my own. Looking for suggestions to fix this.</p> <pre><code>int sum(int number, int min, int counter) { int temp=0, n; n=number+temp; if (number&gt;=(n/2)&amp; number!=min) { while (number&gt;=(n/2)) { cout &lt;&lt; number &lt;&lt; "+"&lt;&lt; temp &lt;&lt;"\n"; number --; temp ++; counter ++; } } sum(temp, 1,counter); return counter; } int main() { int number; cout &lt;&lt; "Please enter the number: "; cin &gt;&gt; number ; cout &lt;&lt; "\n"; sum(number, 1, 0); return 0; } </code></pre> <p>I do realize this is all sorts of messed up.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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