fork(8) download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void Combination(int numInPair[], int indexInPair, int numArray[],int startOfArray, int endOfArray, int maxCount)
  5. {
  6. if(endOfArray == 0 || indexInPair == maxCount) // Termination Condition
  7. {
  8. for(int i = 0; i< maxCount; i++) // Printing Pair's
  9. { // Number One By
  10. cout << numInPair[i]<<" "; // One
  11. } //
  12. cout<<"\n"; // Next Line
  13. }
  14. else
  15. {
  16. for(int i = startOfArray; i < endOfArray; i++)
  17. {
  18. numInPair[indexInPair] = numArray[i];
  19. Combination(numInPair,indexInPair+1,numArray,i+1,endOfArray,maxCount);
  20. // indexInPair+1 : proceding to next position in pair
  21. // i+1 : excluding element by incrementing start of array.
  22. // So in next call we iterate for new start to end
  23. }
  24.  
  25. }
  26. }
  27.  
  28. void Combination(int * intArray,int arrLength, int pairLength)
  29. {
  30. int * arrPair = new int[pairLength];
  31. Combination(arrPair,0,intArray,0,arrLength,pairLength);
  32. delete[] arrPair;
  33. }
  34.  
  35. int main()
  36. {
  37. int arrOfNumbers[] = {1, 2, 3, 4, 5};
  38. int length = sizeof(arrOfNumbers)/sizeof(arrOfNumbers[0]);
  39. int sizeOfPair = 3;
  40. Combination(arrOfNumbers,length,sizeOfPair);
  41. return 0;
  42. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5