Answered step by step
Verified Expert Solution
Question
1 Approved Answer
c + + has to use original functions included Some problem require finding all permutations ( different orderings ) of a set of items. For
c has to use original functions included Some problem require finding all permutations different orderingsof
a set of items. For a set of n items there are n permutations.
For example, given the set there are six permutations:
Write a recursively function that generates all the permutations of a
set of numbers. Use the STL set class for all set operations and the
STL linked list class to store and manipulate each individual
permutation.
The program will require storing a set of permutations of numbers that
you can implement in many ways eg linked list, linked lists of
vector, array, etc. Your program should call the recursive function
with sets of several different sizes, printing the resulting set of
permutation for each.
One solution is to first leave out the nth item in the set.
Recursively find all permutations using the set of n items. If we
insert the nth item into each position for all of these permutations,
then we get a new set of permutations that includes the nth item. The
base case is when there is only one item in the set, in which case the
solution is simply the permutation with the single item.
For example, consider finding all permutations of We leave
the out and recursively find all permutations of the set
This consists of permutations:
Now we insert the into every position for these permutations. For
the first permutation we insert the in the front, between and
and after For the second permutation we insert the in the front,
between and and after
When creating a set containing lists, make sure to place a space
between the last two s or the compiler may get confused. For
example, set defines a set where elements are linked
lists containing elements of type int. the code set
without a space will likely produce a compiler error.
Use the following function declaration.
Uses the permutations function to print all permutations of
the first n whole numbers
void printpermutationsint n;
Recursive function that returns a list contains all of the
permutations of the given set of numbers
set permutationsconst set& numbers;
Helper function for printing the contents of a list
void printlistconst list& v;
Note:
You can also use the STL set class for all set operations and the STL
linked list class to store and manipulate each individual permutation.
Sample Output
:
:
:
:
Permuations of :
:
:
:
:
Permuations of :
:
:
:
:
Permuations of :
:
:
:
:
:
:
Permuations of :
:
:
Permuations of :
:
Permuations of :
Press any key to continue
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started