Note that there are some explanatory texts on larger screens.

plurals
  1. POCompute Absolute values of N integer combinations Combinations
    text
    copied!<p>For a N number (a..N) I am finding set of all combinations in the following way:</p> <pre><code>void create_print_combinations(int *t, int x, int n) { if(x == 0) { char p [2 * r + 2]; memset (p, 0, 2 * r +2); for (int j=c;j&gt;0;j--) if(j == c) sprintf(p, "%d", t[j]); else sprintf(p, "%s,%d", p,t[j]); print_combi(p); } else { for (int i= n; i &lt; r; i++) { t[x] = a[i]; create_print_combinations(t, x-1, i+1); } } } </code></pre> <p>So a call to function like:</p> <pre><code>int main() { unsigned long int start=0, end=0; printf ("\nEnter the a positive integer N:"); scanf("%d", &amp;r); start=time(NULL); a = new int[r]; for (int i = 0;i&lt;r;i++) a[i]=i+1; for(int j=1;j&lt;=r;j++) { a1 = new int[j]; c=j; create_print_combinations(a1, c, 0); delete[] a1; } end=time(NULL); printf("Total time taken = %llu\n" , end - start); return 0; } </code></pre> <p>Gives me combinations like for N=4:</p> <p>Enter the a positive integer N:4</p> <pre><code>Combo : [1] Combo : [2] Combo : [3] Combo : [4] Combo : [1,2] Combo : [1,3] Combo : [1,4] Combo : [2,3] Combo : [2,4] Combo : [3,4] Combo : [1,2,3] Combo : [1,2,4] Combo : [1,3,4] Combo : [2,3,4] Combo : [1,2,3,4] </code></pre> <p>Now my tasks is to fond the absolute values of all combinations like:</p> <p>For Combo [1,2,3,4] it should be:</p> <pre><code>1+2+3+4 = abs(1+2+3+4) 1+2+3-4 = abs(1+2+3-4) 1+2-3-4 = .. 1-2-3+4 = ... </code></pre> <p>Ans so on</p> <p>I am trying the below logic:</p> <pre><code>while(pos &gt; 0) { for(int a=0; a &lt; i; a++) { if(a==0) sprintf(p,"%d", t[a]); else if(a == pos) sprintf(p,"%s%c%d",p, minus, t[a]); else sprintf(p,"%s%c%d",p, plus, t[a]); } print(p); memset (p , 0, 2 * r +2); pos --; } </code></pre> <p>But I beleiev I am doing something wrong as all sets are not getting printed. I am unable to frame the logic though I feel I am near to completion. Below is my whole program:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;ctype.h&gt; #include &lt;time.h&gt; int *a; int *a1; int r; int c; unsigned long int no =1; int stoi(char *var) { int n1 = 0; int n2 = 0; int n3 = 0; char sign=0; while(*var) { if(isspace(*var)) { var++; continue; } while(*var &gt;= '0' &amp;&amp; *var &lt;= '9') { n1=(n1*10) + (*var - '0'); var++; continue; } if(sign == '+') { n2=n2+n1; n1=0; } else if(sign == '-') { n2=n2 - n1; n1=0; } if(*var == '+' || *var == '-') { if(sign == 0) { n2=n1; n1=0; } sign = *var; } var++; } if(sign == 0) return abs(n1); return abs(n2); } void print(char* var) { printf("[Combo %llu.] %s = %d\n" , no++, var, stoi(var)); } void print_combi(char * a) { int t[c]; char *x = NULL; char *y = a; int i=0; while((x=strchr(y, ',')) != NULL) { *x = '\0'; t[i++]=atoi(y); y=x+1; } t[i++]=atoi(y); int count =0; int loop = 0; char p [2 * r + 2]; memset (p , 0, 2 * r +2); char plus = '+'; char minus = '-'; for(int k=0;k&lt;2;k++) { if(k==1) { plus = '-'; minus = '+'; } if(i&gt;1) { for(int a=0; a &lt; i; a++) { if(a==0) sprintf(p,"%d", t[a]); else sprintf(p,"%s%c%d",p, plus, t[a]); } } else if(i==1) { sprintf(p,"%d", t[i-1]); print(p); break; } print(p); memset (p , 0, 2 * r +2); if(i==2) continue; if(i==3 &amp;&amp; k ==1) break; int pos = i-1; while(pos &gt; 0) { for(int a=0; a &lt; i; a++) { if(a==0) sprintf(p,"%d", t[a]); else if(a == pos) sprintf(p,"%s%c%d",p, minus, t[a]); else sprintf(p,"%s%c%d",p, plus, t[a]); } print(p); memset (p , 0, 2 * r +2); pos --; } } } void create_print_combinations(int *t, int x, int n) { if(x == 0) { char p [2 * r + 2]; memset (p, 0, 2 * r +2); for (int j=c;j&gt;0;j--) if(j == c) sprintf(p, "%d", t[j]); else sprintf(p, "%s,%d", p,t[j]); print_combi(p); } else for (int i= n; i &lt; r; i++) { t[x] = a[i]; create_print_combinations(t, x-1, i+1); } } int main() { unsigned long int start=0, end=0; printf ("\nEnter the a positive integer N:"); scanf("%d", &amp;r); start=time(NULL); a = new int[r]; for (int i = 0;i&lt;r;i++) a[i]=i+1; for(int j=1;j&lt;=r;j++) { a1 = new int[j]; c=j; create_print_combinations(a1, c, 0); delete[] a1; } end=time(NULL); printf("Total time taken = %llu\n" , end - start); return 0; } </code></pre> <p>As per the program logic I am computing the combinations as strings and the generating the absolute values of the expression.</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