Thread: Nested do-while ?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    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)
    Last edited by bluefare; 04-28-2012 at 04:38 PM.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    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.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    thank you for reply but , this is too general i can not figure out just only hearing try while or for loops

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I don't really understand...

    Quote Originally Posted by bluefare View Post
    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. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    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
    Last edited by bluefare; 04-28-2012 at 07:30 PM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by msh View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    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. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    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. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    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;
    }
    Last edited by bluefare; 05-20-2012 at 01:44 AM. Reason: missing some part

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with nested ifs
    By marlin in forum C Programming
    Replies: 4
    Last Post: 02-15-2012, 10:17 PM
  2. Nested While
    By skg29 in forum C Programming
    Replies: 4
    Last Post: 10-03-2011, 04:48 AM
  3. Nested Structures Possible?
    By thetinman in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2007, 11:22 AM
  4. nested For loops
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 10:12 AM
  5. Help with using nested For
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 09-29-2003, 10:50 PM