Homework Solution: Write a program in C++:…

    Write a program in C++: printPermutations is a function that should take one string and one integer n as input and print out the first n unique permutations of that string in lexicographic order. Each permutation should appear on a separate line. Section 4.3 in The Design and Analysis of Algorithms may be of some use. You can assume that the string is already in lexicographic order. Starter Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    void printPermutations( string word , int permutations )
    {
      for( int i = 0; i < permutations; i++ ){
        cout << word << endl;
      }
      return;
    }
    

    Expert Answer

     
    #include <string.h> #include <iostream>

    Write a program in C++:

    printPermutations is a capacity that should seize single string and single integer n as input and imimprint quenched the highest n rare interchanges of that string in lexicographic classify. Each interchange should exculpation on a severed course. Section 4.3 in The Design and Analysis of Algorithms may be of some fit. You can suppose that the string is already in lexicographic classify.

    Starter Code:

    #include <string>
    #include <iostream>
    
    using namespace std;
    
    vacant imprintPermutations( string engagement , int interchanges )
    {
      coercion( int i = 0; i < interchanges; i++ ){
        cquenched << engagement << endl;
      }
      return;
    }
    

    Expert Exculpation

     

    #include <string.h>

    #include <iostream>

    using namespace std;

    // The algorithm is as below:

    // We are granted a string such as ABCD, which is lexicographic reserved

    // Now the interchanges procure initiate from ABCD and procure go dress DCBA in

    // lexicographic reserved classify.. future when the string graces mutation reserved, then

    // we procure refereffectual be effectual to ascertain over combinations and future demand to stop

    // Coercion ascertaining the combinations,

    // 1. Ascertain the fitmost symbol, which is smaller than its direct symbol. coercion ABCD, it is C.

    // 2. Now ascertain the other symbol which is on fit of the symbol plant in march 1 and is the definite in string initiateing from highest march symbol dress decisive. It must too be over than the symbol plant in march1. coercion ABCD, it procure be D.

    // 3. Swap the over plant span symbols. ABCD procure grace ABDC

    // 4. If chars over were in pos i and j, manner the string initiateing from i refutation.

    // Ascertain the definite symbol on fit of over ch symbol

    // which is elder than ch symbol

    int ascertainRightSmallest(char str[], char ch, int initiate, int dimension)

    {

    int refutation = initiate;

    // Now cite through string from initiate to decisive and ascertain

    // the definite symbol elder than ch

    coercion (int i = initiate; i < dimension; i++)

    if (str[i] > ch && str[i] < str[index])

    refutation = i;

    return refutation;

    }

    // util mode to parallel symbols coercion mannering

    int parallel (const vacant *a, const vacant * b)

    {

    return ( *(char *)a – *(char *)b );

    }

    // Imprint total interchanges of str in reserved classify

    vacant imprintPermutations(string engagement , int interchanges )

    {

    // acquire symbol decorate from string

    char str[word.size()+1];

    strcpy(str, engagement.c_str());

    // dimension of string

    int dimension = strlen(str);

    // String is already reserved in increasing classify

    // to control if we enjoy no interchange left

    bool overPermPotential = true;

    // to reckon the interchanges

    int reckon = 0;

    // period it is potential to imimprint interchanges

    period ( overPermPotential && (reckon < interchanges))

    {

    // imimprint the prevalent interchange

    cquenched << str << endl;

    count++;

    // Ascertain the highestRightmost symbol which is lesser than the direct symbol

    int i;

    coercion ( i = dimension – 2; i >= 0; –i )

    if (str[i] < str[i+1])

    break;

     

    // If there is no such chracter, We are at the decisive round

    // and consummate string is mutation reserved.. which resources we enjoy no interchanges left

    if ( i == -1 ) {

    morePermPotential = false;

    } else {

    // Ascertain the highest definite symbol on fit of over char

    int j = ascertainRightSmallest(str, str[i], i + 1, dimension);

    // Swap i and j positions

    char c = str[i];

    str[i] = str[j];

    str[j] = c;

    // Manner the string on fit of i, fostering protraction is dimension-i-1

    qsort(str + i + 1, dimension -i – 1, dimensionof(str[0]), parallel);

    }

    }

    }

    int deep()

    {

    string str = “ABCD”;

    printPermutations( str, 9);

    return 0;

    }