Combinations

This is a discussion on Combinations within the C Programming forums, part of the General Programming Boards category; Hi! Let's say we have 200 combinations. Now I want to write a program, that will search for those combinations ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Combinations

    Hi!

    Let's say we have 200 combinations. Now I want to write a program, that will search for those combinations and that will never find two the same combinations, always different. How to do that? Should I search for those combinations in some order?

    If you have an idea, don't hesitate and write it down.

    Thank you in advance.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I do not quite understand what combinations you're talking about.

    A combination of 200 digits? A combination of 200 bits? A combination of 2 numbers each running from 1 to 200?

  3. #3
    Sayeh
    Guest
    Actually, the bruteforce approach, to search n number of combinations is simply indexing by one.

    If you have 200 combinations, then start with the first:

    000

    and increment by one

    001
    002
    003
    ...

    until you reach:

    200

    ---

    enjoy.

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I have 12 files and every file has 50 lines. Now I want that from each file the program will select the first line and then in the second file the second line and so on. I want to generate all combinations.
    Example:

    first file: first line, second file: first line, ..., and then all over again first line: second line, second file: first line, ...

    How to do that?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!

    I just can say that there are 25^12 possibilities.

    klausi
    When I close my eyes nobody can see me...

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    #include <stdio.h>
    int main (void)
    {
     int i[3];
     for (i[0] = 0; i[0] < 6; i[0]++)
      for (i[1] = 0; i[1] < 6; i[1]++)
       for (i[2] = 0; i[2] < 6; i[2]++)
       {
        printf ("%d %d %d\n", i[0], i[1]. i[2]);
       }
     return 0;
    }
    Hope that helps, although in the file example you describe.... well, it looks like you want to process every line in every file, which makes me wonder, why not just process the whole files at once? Could you decribe a bit more what you want to do?
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I have some text in those 12 files. Which file contains 50 lines of text. Now I want to generate all combinations (i think that the number of all combinations is 12^50, is that right?). From each file i select one line and copy it to the other file (in that file i get then 12 lines). I want to generate all combinations.

    QuestionC: please explain a bit more your code. What is that "i" and that 6 (number of files?) in the for loop?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    Unregistered
    Guest
    /* With 12 files of 50 lines, you get 50^12 texts */

    /* Code for 3 files of 5 lines each.
    You get 5^3 = 125 different texts" */

    #include <stdio.h>

    int main(void)
    {
    int i, j, k, count;
    char text1[5][12]={ "text1 line1", "text1 line2", "text1 line3", "text1 line4", "text1 line5"};
    char text2[5][12]={ "text2 line1", "text2 line2", "text2 line3", "text2 line4", "text2 line5"};
    char text3[5][12]={ "text3 line1", "text3 line2", "text3 line3", "text3 line4", "text3 line5"};
    count = 1;
    for(i=0;i<5;i++)
    for(j=0;j<5;j++)
    for(k=0;k<5;k++){
    printf("text number %d\n",count++);
    printf("%s\n",text1[i]);
    printf("%s\n",text2[j]);
    printf("%s\n",text3[k]);
    printf("\n\n");}
    return 0;
    }

  9. #9
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Unregistered:

    You print this to the screen. How to do it that the program will append text from each line into a string?

    Example:

    string[1000];

    now I want that the string will be something like that:

    text1 line1
    text2 line1
    text3 line1

    and then the program must clear the string and appends another combination:

    text1 line1
    text2 line2
    text3 line2

    and so on. How to do that?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    for(i=0;i<5;i++)
    for(j=0;j<5;j++)
    for(k=0;k<5;k++)
    {
    sprintf(texte, "%s\n%s\n%s\n",text1[i],text1[j],text1[k]);
    printf("%s\n",texte);
    getc(stdin);
    }

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    Sorry, it's

    sprintf(texte, "%s\n%s\n%s\n",text1[i],text2[j],text3[k]);

    of course ...

  12. #12
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thanks GertFaller, but I already figure it out yesterday. Now I want to know does sprintf automatically adds '\0' to the end of the whole string?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  13. #13
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    Yes ...

  14. #14
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thanks!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Itīs right that there are 50^12 possible combinations, but if you wanna compare them, there are only 25^12.

    klausi
    When I close my eyes nobody can see me...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating combinations of array values - recursion??
    By johndirect in forum C Programming
    Replies: 2
    Last Post: 11-20-2008, 12:49 PM
  2. Combinations
    By Cmuppet in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 08:39 AM
  3. computing the number of combinations
    By clover in forum C Programming
    Replies: 34
    Last Post: 06-06-2004, 02:12 PM
  4. Grabbing Ctrl and Alt key combinations
    By scorpio_IITR in forum Linux Programming
    Replies: 0
    Last Post: 04-12-2004, 04:01 AM
  5. Combinations, lotto, 6/49
    By Robert in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2002, 07:27 PM

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