// In this exercise you receive n and k and write a code that // iterates over all k-uples (t_1,..., t_k) where // t_1, ..., t_k are in {0,...,n-1} and t_1 <= ... <= t_k // // The idea here is: equal to the other exercise, I'll just change the // next_tuple function to make sure that t_1 <= ... <= t_k. // // Notice that generating all n^k and verifying which are monotone // is VERY inneficient, so you should avoid that solution. #include "stdio.h" int t[100]; // say k <= 100 // This function prints all the tuples. void iterate_and_print (int n, int k) { int i; // initialize t = (0,...,0) for (i = 0; i < k; ++i) t[i] = 0; do { for (i = 0; i < k; ++i) printf("%d ", t[i]); printf("\n"); } while (next_tuple(n,k)); } // This function finds the next tuple. It does like in the example, // it increments one variable, if it < n, I return. If it is n, I // increment the other variable recursively. It returns false if there // is no next permutation. int next_tuple(int n, int k) { int i; for (i = 0; i < k; ++i) { t[i]++; if (i < k-1) { if (t[i] <= t[i+1]) return 1; else t[i] = 0; } else { if (t[i] < n) return 1; else t[i] = 0; } } return 0; } int main() { iterate_and_print(4,4); }