1. ## Nested do-while ?

Code:
```#include <iostream>
#include <algorithm>
using namespace std;

int main () {
int n=4;
int myints[] = {1,2,3,4};
do {

for(int i=0 ; i<n ;i++)
cout << myints[i];
cout<<endl;

} while ( next_permutation (myints,myints+2) );

return 0;
}```
hi , this is the output of the program ;
1 2 3 4
2 1 3 4

now the question is how can i make the output like this ;

1 2 3 4
1 2 4 3
2 1 3 4
2 1 4 3

if you realize first 2 number had made a permutation each other and the other last 2 number had a permutation each other ,

the example is about 4 number making and per 2 of them making permutation ( 2!*2!= 4 possible results )

from here if you give me the idea i will try to make this code for ' n '

like we say there are 9 number and per 3 of them makes permutation,(3! *3!*3! = 108 possible results)

2. Try solving your problem with for or while loops. They're almost always the better choice.

do-while loops often look like a good idea when they, in fact, are a pretty terrible one. Deep meditation is advised before using them.

3. thank you for reply but , this is too general i can not figure out just only hearing try while or for loops

4. I don't really understand...

Originally Posted by bluefare
now the question is how can i make the output like this ;

1 2 3 4
1 2 4 3
2 1 3 4
2 1 4 3

if you realize first 2 number had made a permutation each other and the other last 2 number had a permutation each other ,

the example is about 4 number making and per 2 of them making permutation ( 2!*2!= 4 possible results )
So you are treating the group of 4 numbers as 2 separate groups of 2 and permuting them in turn? If that's what you mean then I don't agree with your number of results - I think it's 2!+2!=4 not 2!*2!=4. So for 9 numbers, 3 groups, we'd permute each set of 3 3! times, over 3 groups, means 3!+3!+3!=18 possible results not 108?

Like so:
Code:
```123456789
132456789
132465789
132465798
213465798
213546798
213546879
231546879
231564879
231564897
312564897
312645897
312645978
321645978
321654978
321654987
123654987
123456987```
Have I misunderstood?

from here if you give me the idea i will try to make this code for ' n '
like we say there are 9 number and per 3 of them makes permutation,(3! *3!*3! = 108 possible results)
So you have 2 variables -- N - number of numbers, and g, number of groups to split them into? Or do you infer the number of groups from N somehow?

If I've got any of that right, then you need 2 loops -- an outer loop that terminates when there aren't any more permutations, and an inner loop which cycles through the groups. I'd use a for loop for the inner loop (convenient to use the index for calculating the offsets to the various groups), and a while loop for the outer loop. Set a flag in the inner loop if no permutations happened, and use that to control the outer loop.

5. Code:
```#include <iostream>
#include <algorithm>
using namespace std;

int main ()
{
int n=9;
int myints[] = {1,2,3,4,5,6,7,8,9};
do
{
do
{
do
{
for(int i=0 ; i<n ; i++)
cout << myints[i];
cout<<endl;
}
while ( next_permutation (myints+6,myints+9) );
}
while ( next_permutation (myints+3,myints+6) );
}
while ( next_permutation (myints,myints+3) );
return 0;
}```

i have done so far but from now on i can not figure how to make recursive do-while loop because the array size can be inceased to 'n' in the example n=9 but later can be maybe 18 or something thanks

it is for 9 => 3!*3!*3! = 108 possible results

6. Originally Posted by msh
do-while loops often look like a good idea when they, in fact, are a pretty terrible one. Deep meditation is advised before using them.
Nonsense, do..while loops are under used IMHO.

They are the solution for when you want the loop to always execute at least once.
I would go so far as to say that using a while loop type when you specifically want the loop to execute at least once, is wrong! setting variables to specific initial states just to force a loop body to be entered is just silly.

At the level of the generated assembly, they are the simplest loop type there is.

bluefare: You either learn to write proper English, or your getting no help from me. I'm not reading that crap several times over just to try and work out where one sentence is supposed to end and another is supposed to start, so that I can make sense of it.

7. Ok, I see now. Thanks for posting your loop code, couldn't figure out what you meant from your first post. Not sure how I'd do it with nested loops, but with recursive functions it should be doable, if a bit fiddly.

You have:

Code:
```            while ( next_permutation (myints+6,myints+9) );
}
while ( next_permutation (myints+3,myints+6) );
}
while ( next_permutation (myints,myints+3) );
return 0;```
So obviously next_permutation(something) will be at the centre of all this. I still don't know how you decide how many groups you'll permute the numbers in. If you had 18 numbers, you could have 2 sets of 9, 9 sets of 2, 3 sets of 6.... you get the idea. If we have G groups and N numbers, we can generalise the code above:

g is current group
groupsize is N/G

Code:
`next_permutation (myints+(g*groupsize),myints+groupsize + (g * groupsize))`
So if I label the groups 0..G-1
Code:
```0      1     2
123   456  789```
Then we can write a function that'll recursively call itself.

Code:
```void processGroup(int* myints, int group, int groups, int n, int groupsize) //plus anything else you need
{

bool perm = false;
do
{
if (g != groups-1) // if not the furthest-right group, call self with next group to the right
processGroup(..., group+1,...)

perm = next_permutation (...);
} while (perm);
}```
You'd call it initially for group 0, then it'd straight away move over to the rightmost group, which is where you wanted to start.

That's the bones of how I'd do it. It wouldn't want to see how it performs on larger values of N. Probably not well -- the recursion isn't deep, but lots of function call overhead could be expensive.

If you do have some way of dynamically determining the size of the groups to permute, then it'll still work, just do that calculation before first call. Sounds like you're just picking at random though.

8. Code:
```#include <iostream>
#include <algorithm>

using namespace std;
int b=0;

void processGroup(int* myints, int group, int n )
{

do
{
if ( group != (n-1) ) //   number of do-while loops

{
b = b + (n-1);             //increase b for next loop , inside of next_permutation(myints +b...)
processGroup(myints,group++,n );

}

else
{
for(int i=0 ; i<n ; i++)      //
cout << myints[i];        //  at the end of the loop  print to screen
cout<<endl;                   //
}
}
while (next_permutation (myints +b, myints+b+b));
}

int main()

{
int group=0;
int n=9;

int myints[] = {1,2,3,4,5,6,7,8,9};

processGroup(myints,group,n);

return 0;
}```
hello i try to do it , but i couldnt

9. ok solved

Code:
```#include <iostream>       //protatip recursive works
#include <algorithm>
using namespace std;

int pro(int *myints,int n,int k)
{

do
{
if(  k== n*(n-1)   )
{
for(int i=0 ; i<12 ; i++)
cout << myints[i];
cout<<endl;
break;
}
pro(myints,n,k+(n-1));

}
while(next_permutation(  myints+k ,  myints+(k + (n-1) )   ));

}

int main()
{
int n=4;  // the " myints[] " size n*(n-1) ,

int myints[] = {1,2,3,4,5,6,7,8,9};
int k=0;
pro(myints,n,k);

return 0;
}```