Possibles Combinations

This is a discussion on Possibles Combinations within the C Programming forums, part of the General Programming Boards category; Hi guys im new here! I just made this code yesterday, but I want the same result with a better ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    5

    Possibles Combinations

    Hi guys im new here!

    I just made this code yesterday, but I want the same result with a better code without using recursion for optimization, i think mine is sloppy. Also im using linux distribution... Any ideas to change this code in 1 or 2 functions?

    Well my program allow to user check for possibles combinations on the alphabet.. and save on file !

    If the user enter 1 as max len ... the output will be all possibles combinations of the alphabet with len 1 , if enter 2 ....

    I did the program with for loop ( but i want it dynamic) if user enter 120 .. all possible combinations.

    example: 1 (26 possibles combinations)

    a
    b
    c
    d
    e
    f
    .
    .
    .
    .
    z

    Example:
    if user enter :: 2 ( 702 possible combinations)

    output will be

    a
    b
    c
    d
    e
    .
    ..
    z
    aa
    ab
    ac
    ad
    ..
    .
    .
    .
    zz

    Anything will help! Thanks. There is my code:

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define Plain "plain.txt" 
    
    
    int length ;
    int i;
    char alphalower[] = "abcdefghijklmnopqrstuvwxyz"; 
    
    
    int main(){
        
            printf("\tEnter Maximum Char Length: ");
                scanf("%d",&length);
                for(i=1;i<=length;i++){
                    combinations(alphalower,i);
                }
        
       return 0;
    }
    
    
    int possibles (const void * a, const void * b)
    {
        return ( *(char *)a - *(char *)b );
    }
    void back2 (char *str, char* info, int last, int index)
    {
        FILE *fp;
        if ((fp = fopen(Plain, "a+")) == NULL)
      {
        perror (Plain);
        exit (EXIT_FAILURE);
      }
        int i, length = strlen(str);
        for ( i=0; i < length; i++ )
        {
            info[index] = str[i] ;
            if (index == last)
                fprintf(fp,"%s\n", info);
            else 
                back2 (str, info, last, index + 1);
        }
        fclose(fp); 
    
    }
    
    int combinations(char *str, int length)
    {
    
        char *info = (char *) malloc (sizeof(char) * (length + 1)) ;
        info[length] = '\0';
        qsort(str, length, sizeof(char), possibles);
        back2 (str, info, length-1, 0);
        free(info);
    
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,622
    Combination

    You may find the above link useful

    EDIT: But I think that's not what you need. Ok, let's say there are only 26 letters, a-z. There will be 26^N possible combinations, where "^" means "at the power of" and "N" is the string's lenght.
    Last edited by GReaper; 02-12-2012 at 07:09 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Program Worked Great I just want to change some the syntax of the code but I can't.

    Any help will be great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. All combinations
    By JonathanS in forum C Programming
    Replies: 38
    Last Post: 10-15-2011, 04:48 PM
  2. N Digit Combinations
    By ziadane127 in forum C Programming
    Replies: 6
    Last Post: 06-20-2010, 10:51 AM
  3. help with combinations !!!!
    By ayan_2587 in forum C Programming
    Replies: 2
    Last Post: 12-12-2009, 11:45 AM
  4. Combinations
    By Cmuppet in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 07:39 AM
  5. Combinations
    By GaPe in forum C Programming
    Replies: 16
    Last Post: 01-09-2002, 04:38 AM

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