Problem: Print all possible combinations of given array with given combination size limit.
Input:
array: 10 11 12 13 14
combination size: 3
Output:
10 11 12
10 12 13
10 13 14
11 12 13
11 13 14
12 13 14
Program:
Input:
array: 10 11 12 13 14
combination size: 3
Output:
10 11 12
10 12 13
10 13 14
11 12 13
11 13 14
12 13 14
Program:
- #include<stdio.h>
- void print_combinations(int a[], int n, int r) {
- int i,j,k;
- for (i = 0; i <= n-r; i++) {
- for (j = i+1; j+r-1 <= n; j++) {
- printf("%d\t", a[i]);
- for (k = 0; k < r-1; k++)
- printf("%d\t", a[j+k]);
- printf("\n");
- }
- printf("\n");
- }
- }
- int main()
- {
- int a[] ={10,11,12,13,14};
- int n=sizeof(a)/sizeof(a[0]);
- print_combinations(a, n, 3);
- return 0;
- }
No comments:
Post a Comment