I need a code that will print all possible combinations and permutations of an array of words (eg. "alpha", "beta", "delta").
The code should be such that it permutates each line from the result of the combination, where 'r' will be the number of elements that will be selected at a time from the array.
#include<iostream>
#include<string>
using namespace std;
void permute(string a[], int l, int r)
{
if (l == r)
{
for(int i=0;i<=r;i++)
cout << a[i] << " ";
cout << endl;
}
else
{
for (int i = l; i <= r; i++)
{
swap(a[l], a[i]);
permute(a, l + 1, r);
swap(a[l], a[i]);
}
}
}
int main()
{
string ArrStr[] = { "alpha", "beta", "delta" };
int r = 3;
permute(ArrStr, 0, r-1);
return 0;
}
Comments
Leave a comment