Every combination possible algorithm

This is a discussion on Every combination possible algorithm within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to figure out a way to store every combination of letters in an array using C. ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    13

    Question Every combination possible algorithm

    Hi,

    I am trying to figure out a way to store every combination of letters in an array using C.

    For example, If i have the word - COMPUTER

    I want to store the following in an array -

    COMPUTRE
    COMPURET
    COMPUETR
    ...
    ect, ect for all 40000 odd combinations.

    No idea where to start

    Any help would be great.

    Thanks

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Start writing down the combinations on paper or in a notepad (like you did for your example) and take note of the steps you are going through. An example might be:

    Step 1 Write the original word.
    Step 2 Pick the last letter of the word
    Step 3 Shift the letter to the left
    Step 4 Write the new variation
    Step 5 Repeat steps 3 and 4 until the letter you are shifting is all the way to the left.
    Step 6 Repeat steps 3, 4 and 5 using the new last letter until there are no more variations.

    Once you work out the steps you can write a program that follows those steps.

    My example is not complete (just a quick example off the top of my head), but if you can figure out why it is not complete you can work on revising the steps to make it complete, or start over and try a different approach.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I imagine you'd sort all the letters first and check for repeat letters, then handle them seperately.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Start with a simple case, say a two letter word, and do what one of the other posters said about doing it by hand and noticing the steps you are taking.

    Pick a three letter word and do the same -- see if any patter occurs in the steps you are taking.

    Finally, try to generalize the steps you are taking.

    Once you get that figured out -- translate it into C code.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I see a recursive solution to this quickly develloping (although I'm not quite sure if it's feasible on a computer...)
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Google: Permutations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21