Answer to Question #219125 in C++ for Bless

Question #219125

Using the code stub below, output all combinations of character variables a, b, and c, in the order shown below. If a = 'x', b = 'y', and c = 'z', then the output is:

xyz xzy yxz yzx zxy zyx

Your code will be tested in three different programs, with a, b, c assigned with 'x', 'y', 'z', then with '#', '$', '%', then with '1', '2', '3’.

#include <iostream>

using namespace std;

int main() {

  char a;

  char b;

  char c;

  cin >> a;

  cin >> b;

  cin >> c;

  /* Your solution goes here */

  cout << endl;

  return 0;

}

1
Expert's answer
2021-07-20T17:04:08-0400


#include<iostream>
#include<bits/stdc++.h>
using namespace std;

void helper(char arr[], int n, int start)
{
	if(start == n)
	{
		for(int i=0; i<n; i++)
		{
			cout<<arr[i];
		}
		cout<<endl;
		return;
	}
	
	for(int i=start; i<n; i++)
	{
		swap(arr[i], arr[start]);
		helper(arr, n,start+1);
		swap(arr[i], arr[start]);
	}
}


int main()
{
	char arr[3];
	for(int i=0; i<3; i++)
	{
		cout<<"Enter char"<<i+1<<" : ";
		cin>>arr[i]; 
	}
	
	helper(arr,3,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
APPROVED BY CLIENTS