Answer to Question #320700 in C++ for Christian

Question #320700

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.

1
Expert's answer
2022-03-30T07:39:11-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog