fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. void swap(int* a, int* b) {
  4. int temp = *a;
  5. *a = *b;
  6. *b = temp;
  7. }
  8. void printArray(int *a, int length) {
  9. printf("[");
  10. int i;
  11. for (i = 0; i < length; i++) {
  12. printf("%d",a[i]);
  13. if (i != length - 1) printf(", ");
  14. }
  15. printf("]\n");
  16. }
  17. void combinations(int *array,int n,int i,int *currSol,int length) {
  18. if (length == i){
  19. printArray(currSol,length);
  20. return;
  21. }
  22. int j;
  23. for (j = 0; j < n; j++) {
  24. currSol[i] = array[j];
  25. combinations(array,n,i+1,currSol,length);
  26. }
  27. return;
  28. }
  29.  
  30. int main() {
  31. int arr[] = {1,2,3};
  32. int sol[4];
  33. combinations(arr,3,0,sol,4);
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
[1, 1, 1, 1]
[1, 1, 1, 2]
[1, 1, 1, 3]
[1, 1, 2, 1]
[1, 1, 2, 2]
[1, 1, 2, 3]
[1, 1, 3, 1]
[1, 1, 3, 2]
[1, 1, 3, 3]
[1, 2, 1, 1]
[1, 2, 1, 2]
[1, 2, 1, 3]
[1, 2, 2, 1]
[1, 2, 2, 2]
[1, 2, 2, 3]
[1, 2, 3, 1]
[1, 2, 3, 2]
[1, 2, 3, 3]
[1, 3, 1, 1]
[1, 3, 1, 2]
[1, 3, 1, 3]
[1, 3, 2, 1]
[1, 3, 2, 2]
[1, 3, 2, 3]
[1, 3, 3, 1]
[1, 3, 3, 2]
[1, 3, 3, 3]
[2, 1, 1, 1]
[2, 1, 1, 2]
[2, 1, 1, 3]
[2, 1, 2, 1]
[2, 1, 2, 2]
[2, 1, 2, 3]
[2, 1, 3, 1]
[2, 1, 3, 2]
[2, 1, 3, 3]
[2, 2, 1, 1]
[2, 2, 1, 2]
[2, 2, 1, 3]
[2, 2, 2, 1]
[2, 2, 2, 2]
[2, 2, 2, 3]
[2, 2, 3, 1]
[2, 2, 3, 2]
[2, 2, 3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