Question #59477

// a program lists all permutations of ABCDEF in which A appears before D

#include <cstring>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}

void permute(char *a, int l, int r)
{
int i;
if (l == r)
cout << "A" << a << endl;
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permute(a, l+1, r);
swap((a+l), (a+i));
}
}
}

int main()
{
char str[] = "BCDEF";
int n = strlen(str);
permute(str, 0, n-1);

return 0;
}

How can I make it work correct?
1

Expert's answer

2016-04-25T12:26:05-0400
#include <cstring>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int temp1 = 0;
char *b, *m;
int temp = 0;
void swap(char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void permute(char *a, int l, int r, char *b, char *m)
{
    int i;
    for (i = l; i <= r; i++)
    {
        for (int j = 0; j <= r; j++) {
            swap((a + l), (a + j));
            b = m;
            while (*b) {
                if (*b == 'D') temp = b - m;
                if (*b == 'A') temp1 = b - m;
                b++;
            }
            if (temp1 > temp) continue;
            cout << a << endl;
        }
    }
}
int main()
{
    char str[] = "ABCDEF";
    int n = strlen(str);
    b = str;
    m = str;
    permute(str, 0, n - 1, b, m);
    system("pause");
    return 0;
}


http://www.AssignmentExpert.com/</cstdlib></cstdio></iostream></cstring>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
27.04.16, 17:58

Dear customer, Unfortunately, your question requires a lot of work and cannot be done for free. Please submit it with all requirements as an assignment to our control panel and we'll assist you.

Salem
27.04.16, 01:01

P(6,4)=360 that means we should get 360 different ways to list all the letters "ABCDEF" were A appears before D. This code just prints 18 outputs !!

LATEST TUTORIALS
APPROVED BY CLIENTS