-1

I have some array with numbers. When I want to write all combination with 3 digits I wrote this. But now I need edit this code to return all combination with 1 to numbers.size digits. How should I edit it ?

int items[] = {1, 2, 3, 4, 5};
int itemSize = 5;


for (int i = 0; i < itemSize - 2; i++) {
    for (int j = i + 1; j < itemSize - 1; j++) {
        for (int k = j + 1; k < itemSize; k++)
            printf("%d%d%d\n", items[i], items[j], items[k]);
    }
}
Shahriar
  • 11,101
  • 5
  • 66
  • 84
hudi
  • 13,245
  • 40
  • 125
  • 230
  • 1
    Did you google before asking SO? – SMA Dec 13 '14 at 10:42
  • yes I just google this example above here – hudi Dec 13 '14 at 10:43
  • Try searching for combination and it will list you everything and has been resolved many times in SO before. – SMA Dec 13 '14 at 10:44
  • "How should I edit it" - I'd start by fixing all the syntax and undeclared identifiers errors; this isn't even valid C code. – WhozCraig Dec 13 '14 at 11:08
  • Is `int[] items = ({1, 2, 3, 4, 5});` C? – bzeaman Dec 13 '14 at 11:32
  • possible duplicate of [Algorithm to return all combinations of k elements from n](http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n) – Joe Jan 26 '15 at 09:47

2 Answers2

0

it is not perfect solution, but it is well enough to work. you can get all sub array from an array, from 1 element to length array

void permute(char* previous, char *a, int i, int n,int nmax)
{
   int j;
   if (i == n)
   {
       char c = a[nmax];
       a[nmax] = 0;
       if (strstr(previous,a) == 0)
       {
           printf("%s\n", a);
           strncpy(previous,a,n);
       }
       a[nmax] = c;
   }
   else
   {
       for (j = i; j < n; j++)
       {
            char c = a[i];
            a[i] = a[j];
            a[j] = c;
            permute(previous,a, i+1, n,nmax);
            c = a[i];
            a[i] = a[j];
            a[j] = c;
       }
   }
}

void subarrays(char *a, int len)
{
    int i=1;
    char *previous = strdup(a);
    *previous = 0;

    for(i=1;i<len+1;i++)
    {
        permute(previous,a,0,len,i);
    }
}
int main(void) {

    char *arr = strdup("abcde");
    subarrays(arr,strlen(arr));


    return EXIT_SUCCESS;
}
Adem
  • 9,146
  • 8
  • 39
  • 57
  • Based on the function name, this is a "next permutation" function, but what the question is asking for is a "next combination" function. – rcgldr Dec 13 '14 at 15:47
0

Since you're already doing a web search for this, here is a generic solution.

#include <stdio.h>

int next_combination(size_t *I, size_t k, size_t n)
{
size_t i, j;
    i = k-1;                            /* find next element to increment */
    while(I[i] == (n-k+i)){
        --i;
        if(i == (size_t)-1){            /* if done */
            for(i = 0; i < k;  i++)     /*  return with initial combination */
                I[i] = i;
            return(0);
        }
    }
    I[i] += 1;                          /* increment element */
    for(j = i+1; j < k; j++)            /* create increasing string */
            I[j] = I[i]+j-i;
    return(1);                          /* return with new combination */
}

int main(int argc, char **argv)
{
int    A[5] = {1, 2, 3, 4, 5};
size_t I[5];
size_t i, k, n;
    n = sizeof(A)/sizeof(A[0]);         /* n things */
    for(k = 1; k <= n; k++){            /* n things k at a time */
        for(i = 0; i < k;  i++)         /* create initial combination */
            I[i] = i;
        do{                             /* display combinations */
            for(i = 0; i < k; i++)
                printf("%2d", A[I[i]]);
            printf("\n");
        }
        while(next_combination(I, k, n));
    }
    return(0);
}
rcgldr
  • 23,179
  • 3
  • 24
  • 50