Thread: Permutation of an array of strings

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    Permutation of an array of strings

    So, after a long search on the internet for a permutation of an array of strings I have given up hope, and tried to make it myself.

    I have gotten pretty far and damn proud of it, but once i am done with the last and second to last lines....it gets really hard. The code and output is given below.

    Right now, I think my knowledge is reaching the limit of what i can do for this program...so I'm going to you for help.

    If you know an algorithm on how to do this please share, or at least help me because each time I go down the list I need another loop and statements to complement it.

    I might need some type of loop expansion that depends on the amount of k? on top of that an expanding list of ints? Just a guess though.

    Please, for the love of god, help me! Thanks :P

    Code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    
        const int n = 11;
        const char list[][3] = {"H","B","C","N","P","O","S","Se","F","Cl","Br","I"};
    
        int k = 5;
        char kArray [4][3] = {"H","H","H","H","H"};
    
        int temp;
        int temp2;
        int ktemp = k;
        int zero = 0;
        int zero2 = 0;
        int count = 1;
    
            for(temp=0;temp<=n;temp++)// temp = ktemp-1;ktemp>=0;ktemp--)
            {
    
    
                for(temp2 = 0;temp2<=n;temp2++) //this loop makes the last on the list chnage to all n forms
                {
                    strcpy(kArray[4-zero2], list[temp2]);
                    printf("%s %s %s %s %s\n", kArray[0], kArray[1], kArray[2], kArray[3], kArray[4]);
                }
    
                zero++;
                strcpy(kArray[4],"H");
                strcpy(kArray[4-count],list[zero]);
    
    
    
            }
        return(0);
    }
    Output:
    Code:
    H H H H H
    H H H H B
    H H H H C
    H H H H N
    H H H H P
    H H H H O
    H H H H S
    H H H H Se
    H H H H F
    H H H H Cl
    H H H H Br
    H H H H I
    H H H B H
    H H H B B
    H H H B C
    H H H B N
    H H H B P
    H H H B O
    H H H B S
    H H H B Se
    H H H B F
    H H H B Cl
    H H H B Br
    H H H B I
    H H H C H
    H H H C B
    H H H C C
    H H H C N
    H H H C P
    H H H C O
    H H H C S
    H H H C Se
    H H H C F
    H H H C Cl
    H H H C Br
    H H H C I
    H H H N H
    H H H N B
    H H H N C
    H H H N N
    H H H N P
    H H H N O
    H H H N S
    H H H N Se
    H H H N F
    H H H N Cl
    H H H N Br
    H H H N I
    H H H P H
    H H H P B
    H H H P C
    H H H P N
    H H H P P
    H H H P O
    H H H P S
    H H H P Se
    H H H P F
    H H H P Cl
    H H H P Br
    H H H P I
    H H H O H
    H H H O B
    H H H O C
    H H H O N
    H H H O P
    H H H O O
    H H H O S
    H H H O Se
    H H H O F
    H H H O Cl
    H H H O Br
    H H H O I
    H H H S H
    H H H S B
    H H H S C
    H H H S N
    H H H S P
    H H H S O
    H H H S S
    H H H S Se
    H H H S F
    H H H S Cl
    H H H S Br
    H H H S I
    H H H Se H
    H H H Se B
    H H H Se C
    H H H Se N
    H H H Se P
    H H H Se O
    H H H Se S
    H H H Se Se
    H H H Se F
    H H H Se Cl
    H H H Se Br
    H H H Se I
    H H H F H
    H H H F B
    H H H F C
    H H H F N
    H H H F P
    H H H F O
    H H H F S
    H H H F Se
    H H H F F
    H H H F Cl
    H H H F Br
    H H H F I
    H H H Cl H
    H H H Cl B
    H H H Cl C
    H H H Cl N
    H H H Cl P
    H H H Cl O
    H H H Cl S
    H H H Cl Se
    H H H Cl F
    H H H Cl Cl
    H H H Cl Br
    H H H Cl I
    H H H Br H
    H H H Br B
    H H H Br C
    H H H Br N
    H H H Br P
    H H H Br O
    H H H Br S
    H H H Br Se
    H H H Br F
    H H H Br Cl
    H H H Br Br
    H H H Br I
    H H H I H
    H H H I B
    H H H I C
    H H H I N
    H H H I P
    H H H I O
    H H H I S
    H H H I Se
    H H H I F
    H H H I Cl
    H H H I Br
    H H H I I
    So output after this should be:
    Code:
    H H B H H
    H H B H B
    H H B H C
    H H B H ...so on
    H H B C H
    H H B C C
    H H B C ...so on
    H H B ... ... so on
    H H C H H
    H H ... ... ... so on
    Hope you understood that :S
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    My advice would be very similar to what I told you here: All combinations.

    You will use 5 nested loops again, but the start and end condition for the loops will be different.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    omfg, you sexy beast! I tried changing it before to do it but failed, now i tried it and I think it works, I'll check the output as soon as it finishes <3
    My Ctrl+S addiction gets in the way when using Code Blocks...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    it didn't work, but modified it even more and it works. If you would like to see it i'll post it.

    Either way thank you so much! you saved me, days, maybe even weeks of time!
    My Ctrl+S addiction gets in the way when using Code Blocks...

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No need to post it unless you have more questions, or want further critiquing. Glad it works. You're welcome.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    No need to post it unless you have more questions, or want further critiquing. Glad it works. You're welcome.
    You sexy beast .... ROFL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  4. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  5. INT ARRAY Permutation!
    By arthur5005 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 05:30 AM