Thread: Program to find combination of groups and letters

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    1

    Program to find combination of groups and letters

    I have to find a combination of groups of letters, second letter in first group should be the same as first letter in second group etc.
    For example, solution for this group: AA, CB, AC, BA, BD, DB is this: CB, BD, DB, BA, AA, AC
    I have this code so far, it works, but if there is a lot of groups, it takes ages to compute. I need to make it more efficient.
    In the input file, there's this input


    10
    C D
    B C
    B B
    B B
    D B
    B B
    C A
    A B
    B D
    D C

    My code

    Code:
    #include<stdio.h>
    Code:
    #include<stdlib.h>
    
    void permutation(char group[][2],int buffer,int sum){
        int i, j;
        char temp;
    
        if(buffer == sum && group[1][1]== group[sum][2]){
            for(i =1; i < sum; i++)
                if(group[i][2]!= group[i+1][1])break;
    
            if(i == sum){
                FILE*output;
                output = fopen("output.txt","a");
                for(j =1; j <= sum; j++){
                    fprintf(output,"%c %c\n", group[j][1], group[j][2]);
                }
                exit(1);
            }
        }else{
            for(i = buffer; i <= sum; i++){
                temp = group[buffer][1];
                group[buffer][1]= group[i][1];
                group[i][1]= temp;
                temp = group[buffer][2];
                group[buffer][2]= group[i][2];
                group[i][2]= temp;
    
                permutation(group, buffer +1, sum);
    
                temp = group[buffer][1];
                group[buffer][1]= group[i][1];
                group[i][1]= temp;
                temp = group[buffer][2];
                group[buffer][2]= group[i][2];
                group[i][2]= temp;
            }
        }
    }
    
    int main(){
        FILE*input;
    
        input = fopen("input.txt","r");
    
        int sum, i;
    
        fscanf(input,"%d",&sum);
    
        char group[sum][2];
    
        for(i =1; i <= sum; i++){
            fscanf(input,"%s",&group[i][1]);
            fscanf(input,"%s",&group[i][2]);
        }
    
        permutation(group,1, sum);}


  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Be sure to paste your code "as text" so the forum is able to apply its own syntax highlighting and line numbering.

    What do you mean by "it takes ages to compute"? Does it take a long time for the given data set you posted? And if so, how long does it take?

    At a glance, I can see you're opening the output file in a recursive function, which means it is being opened multiple times. That might be taking up some time. (And is also not a very good idea.)

    Also, when opening files for reading and/or writing, you should ensure they have opened successfully before attempting to access them. And they should be closed when you're finished with them.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I see you're also receiving help elsewhere: C - find combination of groups and letters - Stack Overflow

    Note that cross-posting in this manner is considered poor etiquette.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If this is an online exercise, please post the link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-01-2015, 09:18 AM
  2. Logic To Find Least Common Number Combination
    By EdMarx in forum C Programming
    Replies: 5
    Last Post: 11-29-2013, 07:16 AM
  3. Find word with uppercase and lowercase letters
    By doia in forum C Programming
    Replies: 9
    Last Post: 07-15-2010, 08:51 PM
  4. C program for generating combination
    By ymadhusoodan in forum C Programming
    Replies: 13
    Last Post: 05-18-2010, 09:54 AM
  5. Combination program problem
    By Garland in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2007, 03:35 PM