Use C++ Using library types and the <algorithm> header The goal of this assignme
ID: 3691720 • Letter: U
Question
Use C++
Using library types and the <algorithm> header
The goal of this assignment is to get more comfortable using the Standard C++ library types and the algorithm header.
Problems
Your assignment is to implement a function that takes as input a set of strings and produces the set of all permutations of that set.
You CAN use any of the standard library types in your implementation, including library functions from the <algorithm> header that generate permutations.
A permutation is defined as an ordered arrangement of a set of objects. For example, the following set of three distinct strings would have six permutations:
Strings: “ab” “c” “de”
Permutations:
1. ab c de
2. ab de c
3. c ab de
4. c de ab
5. de ab c
6. de c ab
ALSO NEED EXTRA CREDIT: Assume that the set of strings may contain duplicates, i.e. your input could be “ab”, “c”, “ab”. This set would produce only two permutations (“ab” “c” and “c” “ab”). Implement the functionality to handle input with duplicates in your function.
Further specification
Your program should read the set of strings from standard input and print the resulting set of permutations should to standard output. You can use the following code as a starting point for testing your implementation:
}
Explanation / Answer
// C program to print all permutations with duplicates allowed
#include <stdio.h>
#include <string.h>
/* This function is used to to swap values at two pointers */
void swapvalues(char *c, char *d)
{
char temp;
temp = *c;
*c = *d;
*d = temp;
}
/* This function is used to print permutations of string
This function takes three parameters-> String, Starting index of the string, Ending index of the string. */
void createpermutations(char *a, int x, int y )
{
int i;
if (x == y)
printf("%s ", a);
else
{
for (i = x; i <= y; i++)
{
swapvalues((a+x), (a+i));
createpermutations(a, x+1, y);
swapvalues((a+x), (a+i)); /This is how we will be backtracking
}
}
}
/* This is the driver program to test above functions */
int main()
{
char string[] = ;
cout<<”enter the string”;
gets(string;
int n = strlen(string);
createpermutations(string, 0, n-1);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.