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 capacity that should presume undivided string and undivided integer n as input and stereotype quenched the pristine n uncommon interchanges of that string in lexicographic manage. Each interchange should reply on a disjoined direction. Section 4.3 in The Design and Analysis of Algorithms may be of some straight. You can presume that the string is already in lexicographic manage.

    Starter Code:

    #include <string>
    #include <iostream>
    using namespace std;
    unsubstantial stereotypePermutations( string promise , int interchanges )
      coercion( int i = 0; i < interchanges; i++ ){
        cquenched << promise << 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 interchanges achieve begin from ABCD and achieve go dress DCBA in

    // lexicographic arranged manage.. future when the string behoves sumerposition arranged, then

    // we achieve not attributoperative attributoperative attributoperative be operative to meet over combinations and future need to stop

    // Coercion meeting the combinations,

    // 1. Meet the straightmost disposition, which is smaller than its direct disposition. coercion ABCD, it is C.

    // 2. Now meet the other disposition which is on straight of the disposition endow in tread 1 and is the meanest in string begining from pristine tread disposition dress definite. It must also be over than the disposition endow in tread1. coercion ABCD, it achieve be D.

    // 3. Swap the aloft endow couple dispositions. ABCD achieve behove ABDC

    // 4. If chars aloft were in pos i and j, quality the string begining from i protest.

    // Meet the meanest disposition on straight of aloft ch disposition

    // which is superior than ch disposition

    int meetRightSmallest(char str[], char ch, int begin, int magnitude)


    int protest = begin;

    // Now relate through string from begin to definite and meet

    // the meanest disposition superior than ch

    coercion (int i = begin; i < magnitude; i++)

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

    protest = i;

    return protest;


    // util system to parallel dispositions coercion qualitying

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


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


    // Stereotype whole interchanges of str in arranged manage

    unsubstantial stereotypePermutations(string promise , int interchanges )


    // obtain disposition rank from string

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

    strcpy(str, promise.c_str());

    // magnitude of string

    int magnitude = strlen(str);

    // String is already arranged in increasing manage

    // to restrain if we enjoy no interchange left

    bool overPermFeasible = true;

    // to sum the interchanges

    int sum = 0;

    // occasion it is feasible to stereotype interchanges

    occasion ( overPermFeasible && (sum < interchanges))


    // stereotype the running interchange

    cquenched << str << endl;


    // Meet the pristineRightmost disposition which is lesser than the direct disposition

    int i;

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

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



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

    // and perfect string is sumerposition arranged.. which resources we enjoy no interchanges left

    if ( i == -1 ) {

    morePermFeasible = false;

    } else {

    // Meet the pristine meanest disposition on straight of aloft char

    int j = meetRightSmallest(str, str[i], i + 1, magnitude);

    // Swap i and j positions

    char c = str[i];

    str[i] = str[j];

    str[j] = c;

    // Quality the string on straight of i, cherishing prolixity is magnitude-i-1

    qsort(str + i + 1, magnitude -i – 1, magnitudeof(str[0]), parallel);




    int deep()


    string str = “ABCD”;

    printPermutations( str, 9);

    return 0;