Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

#include #include #include using namespace std; int compare(const void *a, const void * b) { return (*(char *)a - *(char *)b); } void swap(char* a, char* b) { char t = *a; *a = *b; *b = t; } void...

1 answer below »
#include#include
#include using namespace std;
int compare(const void *a, const void * b)
{
    return (*(char *)a - *(char *)b);
}
void swap(char* a, char* b)
{
    char t = *a;
    *a = *b;
    *b = t;
}
void reverse(char str[], int l, int h)
{
    while (l < h)
    {
        swap(&str[l], &str[h]);
        l++;
        h--;
    }
}
This function finds the index of the smallest character
which is greater than 'first' and is present in str[l..h]
int findCeil(char str[], char first, int l, int h)
{
    int ceilIndex = l;
    
Now iterate through rest of the elements and find
    
the smallest character greater than 'first'
    for (int i = l + 1; i <= h; i++)
    if (str[i] > first && str[i] < str[ceilIndex])
        ceilIndex = i;
    return ceilIndex;
}
void lexiographicPermutations(char P[])
{
    
Get size of string
    int n = strlen(P);
    
Sort the string in increasing order
    qsort(P, n, sizeof(P[0]), compare);
    
Print permutations one by one
    bool isFinished = false;
    while (!isFinished)
    {
        
print this permutation
        cout
P;
        
Find the largest index such p[i]        int i;
        for (i = n - 2; i >= 0; --i)
        if (P[i] < P[i + 1])
            
eak;
        
termination condition,
        
means we just added the last permutation and we are done.
        if (i == -1)
            isFinished = true;
        else
        {
            
largest index such that p[j]>p[i]
            int j = findCeil(P, P[i], i + 1, n - 1);
            cout
" i= "
i
" j= "
j
endl;
            
Swap first and second characters
            swap(&P[i], &P[j]);
            
reverse the string
            reverse(P, i + 1, n - 1);
        }
    }
}
int main(){
    char str[] = "1234";
    lexiographicPermutations(str);
    return 0;
}

Program 2 – Generating Permutations
CSC2400
Assigned: 10/10/2018
Due: 10/26/2018 (by 11:59 PM)
DESCRIPTION
Write a C++ program implemented in a single source file named permutations.cpp that uses two decrease-
and-conquer algorithms for generating permutations. The two algorithms are Johnson Trotter and
Lexicographic Permute. The pseudocode for these algorithms are provided in this assignment. Each
permutation should be printed to the screen in the specific way as the example output at the bottom of this
assignment.
JOHNSON TROTTER
LEXICOGRAPHIC PERMUTE
DON’T CHEAT & GET HELP
Make sure you review the “Get Help & Don’t Cheat!” section of our class syllabus so that you are not charged
with academic misconduct. I am aware that many implementations of these algorithms are available online.
However taking solutions off of websites and forums online and submitting them as your own work is
considered cheating in CSC2400. The assignment is to be able to create your own implementation of these
algorithms, not simply just to find an implementation online.
SAMPLE OUTPUT
Note: I do want you to print out the a
ow directions for the Johnson Trotter
algorithm as shown in the output above. Also, I do want the value for i & j to be3
printed out for the lexicographic algorithm.
WHAT TO TURN IN
Zip your permutations.cpp file and then upload it to the program 2 assignment folder in ilearn by the due
date.
Answered Same Day Oct 17, 2020

Solution

Snehil answered on Oct 22 2020
166 Votes
My Work/permutations.cpp
My...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here