I have 10 Objects(n) I would like a C++ code that could print all the permutations of these 10 Objects in 5 spaces only, not the entire permutations of the objects, for example in an array of size 5.
Asked
Active
Viewed 112 times
-5
-
You want the algorithm or C++ code? They're 2 different things. – user202729 Apr 06 '18 at 08:04
-
the C++ code, if you have it or you could guide me to a link that has it – Mahmoud Abdel-Rahman Apr 06 '18 at 08:09
-
This: http://en.cppreference.com/w/cpp/algorithm/next_permutation could be a starting point. – Bob__ Apr 06 '18 at 08:10
-
I now this code, this code prints all the permutations of a specified string. I need a code that prints the permutations in a specified number of space. For example I have 10 objects and I want to pick 5 objects at a time, I need a code to print the permutations of that. – Mahmoud Abdel-Rahman Apr 06 '18 at 08:13
-
What did you try? What is your specific question? https://stackoverflow.com/help/how-to-ask – Thomas Sablik Apr 06 '18 at 08:16
-
I tried the next_permutation function in C++. This isn't what I want. What I want is a code to print all the permutations of 10 objects(n) in 5 spaces(r). – Mahmoud Abdel-Rahman Apr 06 '18 at 08:19
-
Good, so you can apply that to an array `is_in[] = {1,1,1,1,1,0,0,0,0,0}` and then fill in the "spaces" with the correct objects, and then apply it again to the spaces array... I'm not saying it's the best way, but you can give it a try and build upon that. – Bob__ Apr 06 '18 at 08:20
-
Try to write the code yourself. Post your code and ask a specific question when you get a problem with your code. – Thomas Sablik Apr 06 '18 at 08:22
-
If I knew the code I would've not posted my question. – Mahmoud Abdel-Rahman Apr 06 '18 at 08:22
-
1You might be interested by [implementation-of-permutation-combinations-and-powerset](https://stackoverflow.com/questions/25555683/implementation-of-permutation-combinations-and-powerset-in-c/25556248#25556248), in particular combination. – Jarod42 Apr 06 '18 at 10:20
1 Answers
1
Here is some pseudo code that creates each subset of 5 elements and permutes it:
for z1 = 1:6
for z2 = (z1+1):7
for z3 = (z2+1):8
for z4 = (z3+1):9
for z5 = (z4+1):10
subset = [set[z1], set[z2], set[z3], set[z4], set[z5]]
for i = 1:(5!)
print(subset)
subset = next_permutation(subset)
end
end
end
end
end
end
Here is an example code
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int z1 = 0; z1 < 6; ++z1) {
for (int z2 = z1 + 1; z2 < 7; ++z2) {
for (int z3 = z2 + 1; z3 < 8; ++z3) {
for (int z4 = z3 + 1; z4 < 9; ++z4) {
for (int z5 = z4 + 1; z5 < 10; ++z5) {
std::vector<int> subset {set[z1], set[z2], set[z3], set[z4], set[z5]};
for (int i = 0; i < 120; ++i) {
for (int j = 0; j < 5; ++j)
std::cout << subset[j] << " ";
std::cout << std::endl;
std::next_permutation(subset.begin(), subset.end());
}
}
}
}
}
}
return 0;
}
Instead of 5 loops you can use a recursive function to make the code dynamic.

Thomas Sablik
- 16,127
- 7
- 34
- 62
-
Instead of hard-coding number of permutation (`for (int i = 0; i < 120; ++i)`), use return of `std::next_permutation` (`do { print(subset); } while (std::next_permutation(subset.begin(), subset.end()));`) – Jarod42 Apr 06 '18 at 10:22
-
I can't assure that `std::vector
subset {set[z1], set[z2], set[z3], set[z4], set[z5]};` creates the lexicographically smallest permutation. – Thomas Sablik Apr 06 '18 at 11:11 -
-
I would say it is better to calculate the number of permutations (n!) once than to sort each subset in each loop. – Thomas Sablik Apr 06 '18 at 15:40