Search code examples
cpointerssegmentation-faultconnection-stringmergesort

Ordering no of strings according to given order


If the OrderString is ”dcfbae”, it means d > c > f > b > a > e, unlike the lexicographic(dictionary) ordering where we have a > b > c > d > e > f. all are lower case alphabets.

I'm getting SEG FAULT..i know its somewhat related to pointers but i'm unable to figure it out.. here's my code :

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAX 1001
int min(int x,int y){
    return (x>y)?y:x;
}
int check(char a,char b,char *order){
    int i,x,y;
    for(i=0;i<strlen(order);i++){
        if(a==order[i])
            x=i;
        if(b==order[i])
            y=i;
    }
if(x>y)
    return 1;
else
    return 0;
}
int compare(char *a,char *b,char *order){
   int i=0,l1=strlen(a),l2=strlen(b);
   for(i=0;i<min(l1,l2);i++){
       if(a[i]==b[i])
          continue;
       else{
           if(check(a[i],b[i],order)==1)
              return 1;
           else
              return 0;
       }
   }
}
void merge(int l,int r,int mid,char **string,char *order){
    int size1=mid-l+1;
    int size2=r-mid;
    char a[size1][MAX],b[size2][MAX];
    int i,j,k;
    for(i=0;i<size1;i++)
       strcpy(a[i],string[l+i]);
    for(i=0;i<size2;i++)
       strcpy(b[i],string[mid+1+i]);
    i=0;
    j=0;
    k=0;
    while(i<size1 && j<size2){
       if(compare(a[i],b[j],order)==0){
          strcpy(string[k],a[i]);
          i++;
       }
       else{
          strcpy(string[k],b[j]);
          j++;
       }
    k++;
   }
   while(i<size1){
      strcpy(string[k],a[i]);
      i++;
      k++;
   }
   while(j<size2){
      strcpy(string[k],b[j]);
      j++;
      k++;
   }
 }
 void mergesort(int l,int r,char **string,char *order){
      int mid=(l+r)/2;
      if(l<r){
         mergesort(l,mid,string,order);
         mergesort(mid+1,r,string,order);
         merge(l,r,mid,string,order);
      }
    return ;
 }
int main(){
    char order[MAX];
    scanf("%s",order);
    int T;
    scanf("%d",&T);
    char string[T][MAX];
    int i;
    for(i=0;i<T;i++)
        scanf("%s",string[i]);
    mergesort(0,T-1,string,order);
    for(i=0;i<T;i++)
      printf("%s\n",string[i]);
   return 0; 
  }

i'm using mergesort algorithm.. thanks in advance :)


Solution

  • You declare your mergesort() as follows:

    void mergesort(int l,int r,char **string,char *order)
    

    But then you invoke it as follows:

    mergesort(0,T-1,string,order);
    

    Where string is defined as follows:

    char string[X][Y];
    

    So, this is wrong: your mergesort expects a pointer to an array of pointers to char, and you are passing it an array of char.

    And if your compiler is not giving you an error, or at least a warning for this, then this means that you are trying to develop software without the bare minimum warnings enabled, or you are using your compiler in some compatibility mode for some paleolithic version of C. Which is a lost cause. So, before fixing anything, before even touching your code again, figure out how to enable all warnings on your compiler, or how to get your compiler to compile for the latest C standard.

    To fix this, you could either do what @kkk suggested, which tough might be a bit of work, because you'd have to fix the way your mergesort function works, or you could try to declare your string variable as follows: char** string; Then, you would have to allocate memory for it, with something like this: string = malloc( T * sizeof char* ); and then, right before reading each string, allocate each string like this: string[i] = malloc( (MAX + 1) * sizeof char ); I don't have a C compiler handy, so I can't verify that I got the syntax exactly right, but this should help you get started.