Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Newbie needs helps for printing combinations of integers
    text
    copied!<p>Suppose I am given:</p> <ol> <li>A range of integers <code>iRange</code> (i.e. from <code>1</code> up to <code>iRange</code>) and</li> <li>A desired number of combinations</li> </ol> <p>I want to find the number of all possible combinations and print out all these combinations.</p> <p>For example:</p> <p><strong>Given</strong>: <code>iRange = 5</code> and <code>n = 3</code></p> <p>Then the number of combinations is <code>iRange! / ((iRange!-n!)*n!) = 5! / (5-3)! * 3! = 10</code> combinations, and the output is:</p> <pre><code>123 - 124 - 125 - 134 - 135 - 145 - 234 - 235 - 245 - 345 </code></pre> <p>Another example:</p> <p><strong>Given</strong>: <code>iRange = 4</code> and <code>n = 2</code></p> <p>Then the number of combinations is <code>iRange! / ((iRange!-n!)*n!) = 4! / (4-2)! * 2! = 6</code> combinations, and the output is:</p> <pre><code>12 - 13 - 14 - 23 - 24 - 34 </code></pre> <p>My attempt so far is:</p> <pre><code>#include &lt;iostream&gt; using namespace std; int iRange= 0; int iN=0; int fact(int n) { if ( n&lt;1) return 1; else return fact(n-1)*n; } void print_combinations(int n, int iMxM) { int iBigSetFact=fact(iMxM); int iDiffFact=fact(iMxM-n); int iSmallSetFact=fact(n); int iNoTotComb = (iBigSetFact/(iDiffFact*iSmallSetFact)); cout&lt;&lt;"The number of possible combinations is: "&lt;&lt;iNoTotComb&lt;&lt;endl; cout&lt;&lt;" and these combinations are the following: "&lt;&lt;endl; int i, j, k; for (i = 0; i &lt; iMxM - 1; i++) { for (j = i + 1; j &lt; iMxM ; j++) { //for (k = j + 1; k &lt; iMxM; k++) cout&lt;&lt;i+1&lt;&lt;j+1&lt;&lt;endl; } } } int main() { cout&lt;&lt;"Please give the range (max) within which the combinations are to be found: "&lt;&lt;endl; cin&gt;&gt;iRange; cout&lt;&lt;"Please give the desired number of combinations: "&lt;&lt;endl; cin&gt;&gt;iN; print_combinations(iN,iRange); return 0; } </code></pre> <p><strong>My problem:</strong> The part of my code related to the printing of the combinations works only for <code>n = 2, iRange = 4</code> and I can't make it work in general, i.e., for any <code>n</code> and <code>iRange</code>.</p>
 

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