12. Find all permutations of a given set
This program prints all permutations of a set given by the user. It uses a recursive algorithm to sort out all possible permutations from a set of pitches concatenated in a string, then prints out a table of all the possible sets. The code is implemented as follow:
void Modules::permute(string soFar, string rest, int arrSize, char out, ofstream &outFile) {
if (rest == "") { // convert char symbols from the sub-strings of soFar to pitches, then print out
for (int i = 0; i < arrSize; i++) {
cout << TABLE[charToNum(soFar[i])];
if (out == 'y')
outFile << TABLE[charToNum(soFar[i])];
}
cout << endl;
if (out == 'y')
outFile << endl;
} else {
for (int i = 0; i < static_cast<int>(rest.length()); i++) {
string next = soFar + rest[i];
string remaining = rest.substr(0, i) + rest.substr(i + 1);
permute(next, remaining, arrSize, out, outFile);
}
}
}
13. Find all subsets of a given set
This program prints all subsets of a set given by the user. It uses a recursive algorithm to sort out all possible subsets from a set of pitches concatenated in a string, then prints out a table of all the possible sets. The code is implemented as follow:
void Modules::subsets(string soFar, string rest, char out, ofstream &outFile) {
if (rest == "") { // convert char symbols from the sub-strings of soFar to pitches, then print out
for (int i = 0; i < static_cast<int>(soFar.length()); i++) {
cout << TABLE[charToNum(soFar[i])];
if (out == 'y')
outFile << TABLE[charToNum(soFar[i])];
}
cout << endl;
if (out == 'y')
outFile << endl;
} else {
// add to subset, remove from rest, recursion
subsets(soFar + rest[0], rest.substr(1), out, outFile);
// don't add to subset, remove from rest, recursion
subsets(soFar, rest.substr(1), out, outFile);
}
}