Note that there are some explanatory texts on larger screens.

plurals
  1. POmaintaining a recursion count
    text
    copied!<p>I'm trying to count the number of calls within a recursive permutation function. </p> <p>I've written a function that fills a queue with all the permutations but I can't seem to figure out how to maintain an accurate count.</p> <p>Ultimately i'd like the function to return a subset of the permuatations specified by lbound and ubound arguments, and to do so I think i need someway to keep an internal count.</p> <p>Using the size of the returned queue will not work since i'd like the function to be able to handle permutations too big to hold in memory.</p> <p>For this code i'd like the count to be returned as 100.</p> <pre><code>#include &lt;vector&gt; #include &lt;iostream&gt;; using namespace std; int&amp; Permutations(vector&lt;vector&lt;int&gt;&gt; param, vector&lt;vector&lt;int&gt;&gt; &amp;perm, int index=0) { static vector&lt;int&gt; iter; static int count = 0; if (index == param.size()) { perm.push_back(iter); // add permutation to queue count++; return count; } for (int i=param[index][0]; i&lt;=param[index][1]; i+=param[index][2]) { if (iter.size() &gt; index) iter[index] = i; else iter.push_back(i); Permutations(param, perm, index+1); // recursive function } } void main() { vector&lt;vector&lt;int&gt;&gt; params; // vector of parameter vectors vector&lt;int&gt; param1, param2; int arr1[3] = {0,9,1}; // range for each parameter vector int arr2[3] = {0,9,1}; // specified as lbound, ubound, step param1.insert(param1.end(),arr1,arr1+3); param2.insert(param2.end(),arr2,arr2+3); params.push_back(param1); params.push_back(param2); vector&lt;vector&lt;int&gt;&gt; queue; // queue of generated permutations int permcount = Permutations(params,queue); cout &lt;&lt; "the permutation count is " &lt;&lt; permcount &lt;&lt; endl; cin.get(); } </code></pre>
 

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