Thread: Nested do-while ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

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