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;

    Expert Answer

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

    Write a program in C++:

    printPermutations is a business that should receive individual string and individual integer n as input and sculpture quenched the primitive n choice transpositions of that string in lexicographic classify. Each transposition should show on a disconnected continuity. Section 4.3 in The Design and Analysis of Algorithms may be of some explanation. You can pretend that the string is already in lexicographic classify.

    Starter Code:

    #include <string>
    #include <iostream>
    using namespace std;
    null sculpturePermutations( string term , int transpositions )
      control( int i = 0; i < transpositions; i++ ){
        cquenched << term << endl;

    Expert Reply


    #include <string.h>

    #include <iostream>

    using namespace std;

    // The algorithm is as below:

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

    // Now the transpositions conciliate set-out from ABCD and conciliate go plow DCBA in

    // lexicographic arranged classify.. future when the string behoves inversion arranged, then

    // we conciliate not attributconducive attributconducive attributconducive be conducive to discover past combinations and future demand to stop

    // Control discovering the combinations,

    // 1. Discover the uprightmost repute, which is smaller than its contiguous repute. control ABCD, it is C.

    // 2. Now discover the other repute which is on upupcorrect of the repute institute in tramp 1 and is the meanest in string set-outing from primitive tramp repute plow latest. It must too be past than the repute institute in tramp1. control ABCD, it conciliate be D.

    // 3. Swap the aloft institute couple reputes. ABCD conciliate behove ABDC

    // 4. If chars aloft were in pos i and j, quality the string set-outing from i abjuration.

    // Discover the meanest repute on upupcorrect of aloft ch repute

    // which is superior than ch repute

    int discoverRightSmallest(char str[], char ch, int set-out, int greatness)


    int abjuration = set-out;

    // Now cite through string from set-out to latest and discover

    // the meanest repute superior than ch

    control (int i = set-out; i < greatness; i++)

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

    abjuration = i;

    return abjuration;


    // util regularity to assimilate reputes control qualitying

    int assimilate (const null *a, const null * b)


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


    // Sculpture whole transpositions of str in arranged classify

    null sculpturePermutations(string term , int transpositions )


    // achieve repute adorn from string

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

    strcpy(str, term.c_str());

    // greatness of string

    int greatness = strlen(str);

    // String is already arranged in increasing classify

    // to bridle if we feel no transposition left

    bool pastPermFeasible = true;

    // to number the transpositions

    int number = 0;

    // conjuncture it is feasible to sculpture transpositions

    conjuncture ( pastPermFeasible && (number < transpositions))


    // sculpture the prevalent transposition

    cquenched << str << endl;


    // Discover the primitiveRightmost repute which is lesser than the contiguous repute

    int i;

    control ( i = greatness – 2; i >= 0; –i )

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



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

    // and thorough string is inversion arranged.. which instrument we feel no transpositions left

    if ( i == -1 ) {

    morePermFeasible = false;

    } else {

    // Discover the primitive meanest repute on upupcorrect of aloft char

    int j = discoverRightSmallest(str, str[i], i + 1, greatness);

    // Swap i and j positions

    char c = str[i];

    str[i] = str[j];

    str[j] = c;

    // Quality the string on upupcorrect of i, cherishing extension is greatness-i-1

    qsort(str + i + 1, greatness -i – 1, greatnessof(str[0]), assimilate);




    int deep()


    string str = “ABCD”;

    printPermutations( str, 9);

    return 0;