Hello, everybody!

i have a problem with permutation array.
most algorithm related to permutation, list down all permutation for n!.
however, i would like to generate c++ output as follows:
1234
1243
1342
1324
1423
1432
which is we fix the element '1', and cycle others. and soon. i hope anyone can help me.
thanks

## All 11 Replies

Hello, everybody!

i have a problem with permutation array.
most algorithm related to permutation, list down all permutation for n!.
however, i would like to generate c++ output as follows:
1234
1243
1342
1324
1423
1432
which is we fix the element '1', and cycle others. and soon. i hope anyone can help me.
thanks

Well if you know how to get ALL the permutations of n elements, you know how to get all the permutations where the first element is 1. Get all of the permutations of (2, 3, 4) and stick a 1 in front of each of them.

That's right. actually i'm a beginner in c++. as your suggestion, i dont have idea how to stick element 1 with 2,3,4 or other types fix 2 and stick with 1,3,4.
this is source code as follows: is it enough to get the similar output?
#include<stdio.h>
#include<iostream>
#define clrscr()
using namespace std;
void swap(int*w,int*v)
{int t = *w;
*w = *v;
*v = t;
return ;
}
void sort (int j, int*p)
{ int min, d,i;
for (d=1; d<j; d++){
min=d;
for(i=d+1; i<=j; i++)
if (p<p[min])
min = i;
swap(p+d,p+min);
}
}
void initiate (int n, int x[100])
{int j;
for(j = 0; j<=n;j++){
x[j]=j;
}
return;
}
void write (int n, int x[100])
{int i;
for(i=1;i<=n;i++)
printf("%d",x);
printf("\n");
}
void main()
{int j,n,x[100]
clrscr();
printf("enter the value of n: \n");
scanf_s("%d",&n);
x[0]=n+1;
initiate (n,x);
write(n,x);
swap(x+n,x+j);
}
thanks a lot.

For permutations you could take a look at next_permutation:

( and you shouldn't use void main, use int main and return an int )

That's right. actually i'm a beginner in c++. as your suggestion, i dont have idea how to stick element 1 with 2,3,4 or other types fix 2 and stick with 1,3,4.
this is source code as follows: is it enough to get the similar output?
#include<stdio.h>
#include<iostream>
#define clrscr()
using namespace std;
void swap(int*w,int*v)
{int t = *w;
*w = *v;
*v = t;
return ;
}
void sort (int j, int*p)
{ int min, d,i;
for (d=1; d<j; d++){
min=d;
for(i=d+1; i<=j; i++)
if (p<p[min])
min = i;
swap(p+d,p+min);
}
}
void initiate (int n, int x[100])
{int j;
for(j = 0; j<=n;j++){
x[j]=j;
}
return;
}
void write (int n, int x[100])
{int i;
for(i=1;i<=n;i++)
printf("%d",x);
printf("\n");
}
void main()
{int j,n,x[100]
clrscr();
printf("enter the value of n: \n");
scanf_s("%d",&n);
x[0]=n+1;
initiate (n,x);
write(n,x);
swap(x+n,x+j);
}
thanks a lot.

[code=cplusplus] // paste code here

[/code]

Source code from what? It doesn't appear to have anything to do with permutations. You are going to have to be more specific about what exactly you are asking and what it's for. pr2008 gave you a link to a page that does the work for you, which is great if you're just looking for an answer, but not so great if this is for a school assignment where the object is for you to figure out an algorithm and display the permutations on your own. Where did you get this code? It looks like you just copied and pasted it from somewhere and have no idea what it does or how it works.

And Shamila , Instead of printf and scanf i prefer you use functions like cin and cout int the namespace std;

[code=cplusplus] // paste code here

[/code]

Source code from what? It doesn't appear to have anything to do with permutations. You are going to have to be more specific about what exactly you are asking and what it's for. pr2008 gave you a link to a page that does the work for you, which is great if you're just looking for an answer, but not so great if this is for a school assignment where the object is for you to figure out an algorithm and display the permutations on your own. Where did you get this code? It looks like you just copied and pasted it from somewhere and have no idea what it does or how it works.

That's right. i;m just copy and paste it. i already run three types of permutation algorithm in c++. all the output is list down all permutation ( n!). that's good.

however i'm try generate the output 1234, 1243, 1324, 1342, 1423, 1432 first. others permutation, i put aside / ignore first.
then how to set up it by c++?
for example:
x[1] = 1;
i just have to swap element 2,3,4.

many example is swap all element include 1.

many thanks to you all!

That's right. i;m just copy and paste it. i already run three types of permutation algorithm in c++. all the output is list down all permutation ( n!). that's good.

however i'm try generate the output 1234, 1243, 1324, 1342, 1423, 1432 first. others permutation, i put aside / ignore first.
then how to set up it by c++?
for example:
x[1] = 1;
i just have to swap element 2,3,4.

many example is swap all element include 1.

many thanks to you all!

Well, the code you posted doesn't appear to have anything to do with generating permutations. All it does is sort numbers as far as I can tell. I don't know about the three permutation algorithms that you say worked for you and whether you have source code for them, but the code you posted doesn't do permutations, so I'm not sure why you posted it. If you have permutation code, you're better off trying to tweak it to your needs rather than what you posted. I can't see how the code above will be all that helpful to your task. I'd ditch it. It's not really C++ code anyway.

Hello, many thanks to you

below is my own algorithm

``````#include<iostream>
using namespace std;

int factorial(int n)
{ int i=1,current=1;
while (current<=n)
{ i *= current;
current++;
} return (i);
}

void initiate(int n, int x[100])
{int j;
for(j=0;j<=n;j++){
x[j]=j;}
}
void write (int n, int x[100])
{int i;
for (i=1; i<=n; i++)
printf("%d",x[i]);
printf("\n");}
//
//

void main()
{ int j,i,n,x[100];
cout <<"enter the value of n:\n" << endl;
cin >> n;
initiate(n,x);
i = factorial(n);
cout<<"Number of permutation =\n"<< i << endl;
write (n,x);
}
``````

output:

``````n = 4
Number of permutation =
24
1234
``````

so how to set up swap/sort function in order to get output 1234, 1243, 1342, 1324,1423,1432 only.

thanks again

Hello, many thanks to you

below is my own algorithm

#include<iostream>
using namespace std;

int factorial(int n)
{ int i=1,current=1;
while (current<=n)
{ i *= current;
current++;
} return (i);
}

void initiate(int n, int x[100])
{int j;
for(j=0;j<=n;j++){
x[j]=j;}
}
void write (int n, int x[100])
{int i;
for (i=1; i<=n; i++)
printf("%d",x);
printf("\n");}
//
//

void main()
{ int j,i,n,x[100];
cout <<"enter the value of n:\n" << endl;
cin >> n;
initiate(n,x);
i = factorial(n);
cout<<"Number of permutation =\n"<< i << endl;
write (n,x);
}

output: n = 4
Number of permutation =
24
1234

so how to set up swap/sort function in order to get output 1234, 1243, 1342, 1324,1423,1432 only.

thanks again

Close on the code tags, but not quite. Make sure there are no spaces anywhere. Hit "Preview Post" to make sure it works:

[code=cplusplus] // paste code here

[/code]

The code you posted sorts and displays the number of permutations. It DOES NOT list the permutations, which is what you need. There are lots of permutations threads on daniweb and google, for all skill levels. Type "permutations" or "display permutations" and similar strings into Daniweb's search engine to get some ideas on how to list all the permutations. Solve that problem before tackling your specific problem, since your solution will need a function that lists permutations. Here is the code you posted, formatted and changed to C++ rather than C.

``````#include<iostream>
using namespace std;

int factorial(int n)
{
int i=1,current=1;
while (current<=n)
{
i *= current;
current++;
}
return i;
}

void initiate(int n, int x[100])
{
int j;
for(j=0;j<=n;j++)
{
x[j]=j;
}

}

void write (int n, int x[100])
{
int i;
for (i=1; i<=n; i++)
cout << x[i];

cout << endl;
}

int main()
{
int j,i,n,x[100];
cout <<"enter the value of n:\n" << endl;
cin >> n;
initiate(n,x);
i = factorial(n);
cout<<"Number of permutation =\n"<< i << endl;
write (n,x);

// use these three lines below instead of system ("PAUSE");
cout << "Press Enter key to exit";
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
cin.get ();
return 0;
}``````

Edit: I thought you had used `system ("PAUSE")` , but I guess you didn't. Still, you can use the three lines above to pause the program at the end if you need to. Not really relevant to your particular thread, but it might be useful knowledge.

Close on the code tags, but not quite. Make sure there are no spaces anywhere. Hit "Preview Post" to make sure it works:

[code=cplusplus] // paste code here

[/code]

The code you posted sorts and displays the number of permutations. It DOES NOT list the permutations, which is what you need. There are lots of permutations threads on daniweb and google, for all skill levels. Type "permutations" or "display permutations" and similar strings into Daniweb's search engine to get some ideas on how to list all the permutations. Solve that problem before tackling your specific problem, since your solution will need a function that lists permutations. Here is the code you posted, formatted and changed to C++ rather than C.

``````#include<iostream>
using namespace std;

int factorial(int n)
{
int i=1,current=1;
while (current<=n)
{
i *= current;
current++;
}
return i;
}

void initiate(int n, int x[100])
{
int j;
for(j=0;j<=n;j++)
{
x[j]=j;
}

}

void write (int n, int x[100])
{
int i;
for (i=1; i<=n; i++)
cout << x[i];

cout << endl;
}

int main()
{
int j,i,n,x[100];
cout <<"enter the value of n:\n" << endl;
cin >> n;
initiate(n,x);
i = factorial(n);
cout<<"Number of permutation =\n"<< i << endl;
write (n,x);

// use these three lines below instead of system ("PAUSE");
cout << "Press Enter key to exit";
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
cin.get ();
return 0;
}``````

Edit: I thought you had used `system ("PAUSE")` , but I guess you didn't. Still, you can use the three lines above to pause the program at the end if you need to. Not really relevant to your particular thread, but it might be useful knowledge.

thank you so much. that's help me.

To VernonDozier; thank you so much.

i would like to know your idea/ suggestion about how can i fix '1' element in my permutation using c++ from random order 3,4,2,1 we just to list a arry start with