Thread: Random char Array

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Random char Array

    Dear all,

    i want to randomize the numbers of a char array, like:

    if a have a number 1234. With the function itoa i will have an array like this:
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    a[3] = 4;

    This work fine. Now i want to generate ALL new integers which are made of these values:

    1234, 1243, 1324, 1342, 1423, 1432
    2134, 2143, 2314, 2341, 2413, 2431
    3124, 3142, 3214, 3241, 3412, 3421
    4123, 3132, 4213, 4231, 4312, 4321

    Do you have an idea how to do this?

    Regards

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I have some ideas, but I REALLY want to see what solution you were thinking about; some pseudo-code will be fine.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    if i think in the way as i as a human did:

    1. start with low to hight (element1-element2-element3-element4)

    2. element 1 & element 2 stays the same, interchange element 3 & element 4

    3. element 1 & element 3, put element 3 & element 4

    4. element 1 & element 3, interchange element 3 & element 4

    5. element 1 & element 4, put element 3 and element 4

    6. element 1 & element 4, interchange element 3 & element 4




    7. element 2 & element 1, put element 3 & element 4

    8. element 2 & element 1, interchange 3 & element 4

    9. element 2 & element 3, put element 1 & element 4

    10. element 2 & element 3, interchange 1 & element 4

    11. element 2 & element 4, put element 1 & element 3

    12. element 2 & element 4, interchange 1 & element 3


    and keep going like this

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    So you have a set of 4 elements (1, 2, 3, 4) and you need to find all of it's permutations?

    Have fun googling "c permutations".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM